2011-05-14 99 views
0

我想模擬一個「用戶可以有0或1組首選項」,其中首選項表有一個UserId的主鍵也是一個外部用戶實體的密鑰,la this postEF4.1如何建模0:1(1:1)的關係

我希望我的模型是這樣的:

public class User 
    { 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public virtual int Id { get; set; } 

    [Required] 
    public virtual string Username { get; set; } 

    public virtual UserPreferences Preferences { get; set; } 

    } 

    public class UserPreferences 
    { 
    [Key] 
    public User User { get; set; } 

    public bool SubscribedToNewsLetter{ get; set; } 
    } 

與配置:

HasOptional(u => u.Preferences).WithRequired(l => l.User); 

產量:

SetUp : System.Data.Entity.ModelConfiguration.ModelValidationException : One or more validation errors were detected during model generation: 

    System.Data.Edm.EdmEntityType: : EntityType 'UserPreferences' has no key defined. Define the key for this EntityType. 
    System.Data.Edm.EdmEntitySet: EntityType: EntitySet �UserPreferences� is based on type �UserPreferences� that has no keys defined. 

回答

1

您必須UserPreferences定義鍵:

public class UserPreferences 
{ 
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int UserId { get; set; } 
    public User User { get; set; } 

    public bool SubscribedToNewsLetter{ get; set; } 
} 
0

這裏是我結束了,讓我正是我一直在尋找:

public class User 
    { 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public virtual int Id { get; set; } 

    [Required] 
    public virtual string Username { get; set; } 

    public virtual UserPreferences Preferences { get; set; } 

    } 

    public class UserPreferences 
    { 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int UserId { get; set; } 

    [ForeignKey("UserId")] 
    public User User { get; set; } 

    [Required] 
    public string Password { get; set; } 
    } 

    public class UserConfiguration : EntityTypeConfiguration<User> 
    { 
    public UserConfiguration() 
    {  
     HasOptional(u => u.Preferences).WithRequired(up => up.User); 
    } 
    } 

public class UserPreferenceConfiguration : EntityTypeConfiguration<UserPreferences> 
    { 
    public UserPreferenceConfiguration() 
    { 
     HasRequired(u => u.User).WithOptional(ua => ua.Preferences); 
    } 
    } 

產量:

CREATE TABLE [dbo].[Users](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [Username] [nvarchar](max) NOT NULL, 
    [DeActivatedDate] [datetime] NULL, 
    [IsActive] [bit] NULL, 
PRIMARY KEY CLUSTERED 
(
    [Id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 
GO 
/****** Object: Table [dbo].[UserPreferences] Script Date: 05/14/2011 14:36:51 ******/ 

CREATE TABLE [dbo].[UserPreferences](
    [UserId] [int] NOT NULL, 
    [Password] [nvarchar](max) NOT NULL, 
PRIMARY KEY CLUSTERED 
(
    [UserId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 
GO 
/****** Object: ForeignKey [User_Preferences] Script Date: 05/14/2011 14:36:51 ******/ 
ALTER TABLE [dbo].[UserPreferences] WITH CHECK ADD CONSTRAINT [User_Preferences] FOREIGN KEY([UserId]) 
REFERENCES [dbo].[Users] ([Id]) 
GO 
ALTER TABLE [dbo].[UserPreferences] CHECK CONSTRAINT [User_Preferences] 
GO