无法在应用程序用户表和我的表之间创建关系

时间:2018-06-25 13:05:09

标签: entity

我首先使用实体​​框架代码。下面不断排除无法确定关联的主要目的。基本上,我无法弄清楚如何首先使用代码在自动生成的身份表和我自己的表之间建立一对多的关系。

public class applicationUser : Identity
{

public User user {get;set;}

}

//and then a dependant class called User

public class User
{
public in Id {get;set;}
public string Name {get;set;}

[ForeignKey("Identity")]
public string IdentityId {get;set;}
public ApplicationUser Identity {get;set;}

public string UserPicLocation {get;set;}

}

2 个答案:

答案 0 :(得分:0)

您使用[ForeignKey()]错误。 [ForeignKey()]的值应该是包含引用对象的键的属性的名称。
您还应该使用[Key]将包含所引用对象中的键的属性标记为私钥。 试试这个:

public class applicationUser : Identity
{
    public int UserId {get; set;}
    [ForeignKey("UserId")]
    public User user {get;set;}

}

and then another class called User

public class User
{
    [Key]
    public int Id {get;set;}
    public string Name {get;set;}

    public string IdentityId {get;set;}
    public ApplicationUser Identity {get;set;}

    public string UserPicLocation {get;set;}    
}

答案 1 :(得分:0)

我通过将主键设置为依赖表中的主键和外键来修复它

相关问题