Entity framework 如何向ASP.NETMVC5标识添加外键引用?
我有一个使用ASP.NET标识的新ASP.NET MVC 5.1项目。 它看起来很可靠,很有前途,但今天我花了将近9个小时来做一件简单的事情,如果使用SQL的话 我的问题是,我无法通过CodeFirst创建一个带有默认AspNetUsers表外键引用的表 例如: 我已经创建了一个名为-地址的表Entity framework 如何向ASP.NETMVC5标识添加外键引用?,entity-framework,Entity Framework,我有一个使用ASP.NET标识的新ASP.NET MVC 5.1项目。 它看起来很可靠,很有前途,但今天我花了将近9个小时来做一件简单的事情,如果使用SQL的话 我的问题是,我无法通过CodeFirst创建一个带有默认AspNetUsers表外键引用的表 例如: 我已经创建了一个名为-地址的表 public class Address { [Key] public string UserId { get; set; } public
public class Address
{
[Key]
public string UserId { get; set; }
public string MyAddress { get; set; }
}
但是如何创建对AspNetUsers的外键引用呢
我尝试用替换上面的属性
public IdentityUser MyAddress { get; set; }
// Or
public virtual IdentityUser MyAddress { get; set; }
// Or
public virtual ApplicationUser MyAddress { get; set; }
但它们都显示出错误:
One or more validation errors were detected during model generation:
MiaPos.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
MiaPos.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
我还尝试从以下位置覆盖模型创建上的
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasKey(l=>l.UserId);
modelBuilder.Entity().HasKey(r=>r.Id);
modelBuilder.Entity().HasKey(r=>new{r.RoleId,r.UserId});
}
但是,当我运行添加迁移时,它会创建IdentityUserLogin、IdentityRole、IdentityUserRole表并复制,只是前缀不同。(无网络身份)
最后,我用SQL在10秒内完成了这项工作,但我只想知道为什么我不能用CodeFirst完成这项工作?
为什么微软2014年的新技术这么难做到这一点呢
感谢你Gábor Plez对上述评论 我找到了解决办法 继承IdentityUser(创建AspNetUsers表)的ApplicationUser类应创建子类(子表)的ICollection属性 e、 g 公共虚拟ICollection ToDoes{get;set;} 因此,ToDo类可以引用到ApplicationUser 强烈推荐查看样本,如Gábor Plez所说
你可以这样做。因为有时您可能需要1-1连接
public class AnotherTable
{
[Key]
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
}
如果您真的想创建一个名为Address的表,而不是创建一个名为Address的属性作为列生成到AspNetUsers表中,请执行以下操作(obs:在本例中,我假设一个用户可以有多个地址):
public class Address
{
public string MyAddress { get; set; }
// referencing the AspNetUsers table ( ApplicationUser.cs)
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
}
public virtual ICollection<Address> Addresses { get; set; }
及之后:
Update-Database.
但是,如果您只想将一个新属性包含到AspNet用户中,只需在ApplicationUser.cs文件/类中创建此属性:
public class ApplicationUser : IdentityUser
{
public string Address { get; set; }
}
并为PMC运行相同的命令:
Add-Migration UserAddress
之后:
Update-Database
希望这有帮助 你可以检查这个:谢谢你,Gábor Plez,我会尽快检查这个。嗨,Gábor Plez,我只是检查一下样品。我找到了答案。因此,我回答了我的问题。刚刚发布了一个类似问题的答案,如果这有帮助的话:谢谢你得到了解决方案。请将你的答案标记为帮助的正确答案others@Cheung如果我的实体在另一个类库项目中,而标识表在web项目中,如何添加外键关系?如果您有任何想法,请让我知道?我是否需要将IdentityUser更改为ApplicationUser,无论使用哪种IdentityUser?这应该是正确的答案!对于非1-1,创建第三个表,其中包含另一个表ID和ApplicationUserID@MichaelMao只是按照惯例。将您的子类外键字符串属性命名为“ApplicationSerid”。根据惯例,这将是外键。EntityName+Id。同样对主键有效。'Id'将始终是主密钥。我是否需要将IdentityUser更改为ApplicationUser(使用IdentityUser的位置)?
Add-Migrations UserAddress
Update-Database.
public class ApplicationUser : IdentityUser
{
public string Address { get; set; }
}
Add-Migration UserAddress
Update-Database