2016-11-09 37 views
0

我有这两个实体:如何实现一到一个代码第一种方法,如果我们有自动生成的ID

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

    public string IdsrvUniqueId { get; set; } 

    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Email { get; set; } 

    public string About { get; set; } 
    public GenderEnum Gender { get; set; } 

    public DateTime BirthDate { get; set; } 
    public string PhoneNumber { get; set; } 
} 


public class NotificationSetting 
{  
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid Id { get; set; } 

    public bool AutomaticallySubscribeToAllGroups { get; set; } 

    public bool AutomaticallySubscribeToAllGroupsWithTag { get; set; } 
} 

我想要做的是implemente 1:他们之间,但目前与1间的关系没有成功。我试图用数据符号和装饰他们看起来像这样:

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

     .......... 

     // Navigation properties 
     public virtual NotificationSetting NotificationSettings { get; set; } 
    } 

public class NotificationSetting 
    {  
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     [ForeignKey("User")] 
     public Guid Id { get; set; } 

     .... 

     // Navigation properties 
     public virtual User User { get; set; } 
    } 

它编译,生成迁移文件,但是当我尝试添加新的用户,我得到一个错误。 你能告诉我这里有什么问题吗?我怎样才能实现我的目标。

回答

0

我认为ForeignKey属性是在错误的属性上。

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

    .......... 

    // Navigation properties 
    public virtual NotificationSetting NotificationSettings { get; set; } 
} 

public class NotificationSetting 
{  
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid Id { get; set; } 

    .... 
    [ForeignKey("User")] 
    public Guid UserId { get; set; } 
    // Navigation properties   
    public virtual User User { get; set; } 
} 

这就是我总是定义它们的方法。您发布它的方式,它使用NotificationSetting的PK作为用户的FK

+0

善有善报在用户实体?那里没有任何数据符号吗? – user2128702

+0

用户保持与您的相同。使用User和NotificationSetting更新我的回答 – Mats391

+0

尝试生成我的迁移时,出现此错误:NotificationSetting_User_Source::多重性在'NotificationSetting_User'关系中的角色'NotificationSetting_User_Source'中无效。因为“依赖角色”属性不是关键属性,所以“依赖角色”的多重性的上界必须为“*”。 – user2128702

0

当您将实体配置为具有一对一关系时,您的从属实体中的密钥(NotificationSetting)是主体实体的外键(User)也一样,所以它不能自动生成数据库字段,所以在NotificationSetting[DatabaseGenerated(DatabaseGeneratedOption.Identity)]应删除:

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

    .......... 

    // Navigation properties 
    public virtual NotificationSetting NotificationSettings { get; set; } 
} 

public class NotificationSetting 
{  
    [Key] 
    [ForeignKey("User")] 
    public Guid Id { get; set; } 

    .... 

    // Navigation properties 
    public virtual User User { get; set; } 
} 
+0

我所做的是删除自动生成的ID的两侧,它的工作。我也会尝试这一个。 – user2128702

相关问题