实体框架 – 如何添加外部参考ASP.Net MVC 5身份?
|
我有一个新的ASP.NET MVC 5.1项目使用ASP.Net身份。
我的问题是,我不能通过CodeFirst创建一个表,外键引用默认的AspNetUsers表。 例如: 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. 我也尝试从这个post覆盖OnModelCreating protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId,r.UserId });
}
但是当我运行Add-Migration时,它创建了IdentityUserLogin,IdentityRole,IdentityUserRole表,并且它重复,只是前缀不同。 (AspNet身份) 最后我在十秒钟内通过SQL进行操作,但是我只想知道为什么我不能在CodeFirst中执行 解决方法谢谢你GáborPlesz上面的评论。我找到了解决方案。 继承IdentityUser(创建AspNetUsers表)的ApplicationUser类应该创建子类(子表)的ICollection属性。 例如 public virtual ICollection<ToDo> ToDoes { get; set; }
所以ToDo类可以引用ApplicationUser GáborPlesz表示强烈推荐看样品。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 以URL结尾的问题
- ASP.NET中的401.2的customerrors
- 如何在ASP.NET MVC中配置3个级别的URL?
- asp.net – MVC 6 WebAPI返回序列化的HttpResponseMessage而
- ASP.NET 5 OAuth承载令牌认证
- asp.net-mvc-4 – Require.js优化vs asp.net mvc 4绑定和缩
- asp.net-web-api – 从响应中删除标题
- asp.net – 你可以从请求变量确定时区吗?
- 你能在每个动作上在ASP.NET-MVC中应用ActionFilter吗?
- asp.net-mvc – 处理CORS预检请求到ASP.NET MVC操作
