2011-05-14 96 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