EF4.1 如何建模 0:1 (1:1) 关系

Posted

技术标签:

【中文标题】EF4.1 如何建模 0:1 (1:1) 关系【英文标题】:EF4.1 how to model 0:1 (1:1) relationships 【发布时间】:2011-05-14 13:11:59 【问题描述】:

我正在尝试对“用户可以有 0 或 1 组首选项”进行建模,其中首选项表的主键 UserId 也是用户实体的外键,例如 this post。

我希望我的模型类似于:

  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; 

【讨论】:

【参考方案2】:

这就是我最终得到的结果,这正是我想要的:

  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

【讨论】:

以上是关于EF4.1 如何建模 0:1 (1:1) 关系的主要内容,如果未能解决你的问题,请参考以下文章

如何建模多对多关系?

如何在关系数据库中进行继承建模?

如何在 MongoDB 中建模关系? [复制]

如何在维度模式中建模父子关系表

CoreData:如何建模循环多对多关系

我如何建模这种关系(避免循环)?