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

时间:2011-05-14 13:11:59

标签: mapping entity-framework-4.1 code-first one-to-one

我正在尝试建模“用户可以有0或1组首选项”,其中首选项表具有UserId的主键,UserId也是用户实体的外键,la 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; }
  }

with config:

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.

2 个答案:

答案 0 :(得分:1)

您必须在UserPreferences中定义一个键:

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

    public bool SubscribedToNewsLetter{ get; set; }
}

答案 1 :(得分: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