【问题标题】:mvc 5 code first userid as a Foreign Keymvc 5 代码第一个用户 ID 作为外键
【发布时间】:2014-02-23 07:58:44
【问题描述】:

我有一张桌子:

public class TestModel
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public string Name { get; set; }
}

我希望 UserId 列是外键,Mvc Membership 的用户 ID 列。 我怎样才能做到这一点?

我的身份模型:

public class ApplicationUser : IdentityUser
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

【问题讨论】:

    标签: asp.net-mvc ef-code-first code-first asp.net-mvc-5


    【解决方案1】:

    添加对ApplicationUser的引用并指定ForeignKey:

    public class TestModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public string UserId { get; set; }
        [ForeignKey("UserId")]
        public ApplicationUser User { get; set; }
    }
    

    将新模型添加到您的 DbContext:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<TestModel> TestModels { get; set; }
    
        /* rest of class */
    }
    

    而且您应该做好准备(减少迁移/数据库更新)。

    【讨论】:

    • 谢谢,这也可以反过来工作,如果您希望 AspNetUsers 表引用另一个具有外键的表,请将相同的代码添加到 ApplicationUser 类以更改相关类型。只需确保在另一个表中定义了一个键列。
    • @brad 如果我的实体在另一个类库项目中并且身份表在 Web 项目中,如何添加外键关系?如果您有任何想法,请告诉我?
    【解决方案2】:

    我不喜欢在TestModel 中使用[ForeignKey] 属性的想法。最好使用 Fluent API 执行此操作。这是在TestModel 表中添加UserId 列并添加外键约束的代码:

    public class TestModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }
    }
    
    public class ApplicationUser : IdentityUser
    {
        public virtual TestModel TestModel { get; set; }
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext() : base("DefaultConnection")
        {
        }
    
        public DbSet<TestModel> TestModels { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<ApplicationUser>()
                .HasOptional(m => m.TestModel)
                .WithRequired(m => m.ApplicationUser)
                .Map(p => p.MapKey("UserId"));
        }
    }
    

    【讨论】:

      【解决方案3】:

      在使用 Visual Studio 2015 搭建脚手架时,您可能会遇到错误 CS1061,Ater 遵循了很好的答案: /*************************/

      public class TestModel
      {
          public int Id { get; set; }
          public string Name { get; set; }
      
          public string UserId { get; set; }
          [ForeignKey("UserId")]
          public ApplicationUser User { get; set; }
      }
      

      将新模型添加到您的 DbContext:

      public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
      {
          public DbSet<TestModel> TestModels { get; set; }
      
          /* rest of class */
      }
      

      /**********************************/ 解决方案 删除受影响控制器的控制器和视图, 在 DbContext 中重命名为 看起来像这样

      public class ApplicationDbContext : IdentityDbContext<Users>
      

      重启VS,然后脚手架。

      免责声明:我不是上述解决方案的作者,只是为了某人的利益而编辑和发布

      【讨论】:

        猜你喜欢
        • 2015-08-03
        • 2014-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-09
        • 2013-03-26
        相关资源
        最近更新 更多