Asp.net mvc mvc 5将第一个用户ID编码为外键

Asp.net mvc mvc 5将第一个用户ID编码为外键,asp.net-mvc,ef-code-first,code-first,asp.net-mvc-5,Asp.net Mvc,Ef Code First,Code First,Asp.net Mvc 5,我有一张桌子: public class TestModel { public int Id { get; set; } public string UserId { get; set; } public string Name { get; set; } } 我希望UserId列是一个外键,Mvc成员的用户Id列。 我怎样才能做到这一点 我的身份模型: public class ApplicationUser : IdentityUser { } public cl

我有一张桌子:

public class TestModel
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public string Name { get; set; }
}
我希望UserId列是一个外键,Mvc成员的用户Id列。 我怎样才能做到这一点

我的身份模型:

public class ApplicationUser : IdentityUser
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}
公共类应用程序用户:IdentityUser
{
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“默认连接”)
{
}
}

添加对
应用程序用户的引用并指定外键:

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; }
}
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 */
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<TestModel> TestModels { get; set; }

    /* rest of class */
}
public类ApplicationDbContext:IdentityDbContext
{
公共DbSet TestModels{get;set;}
/*其他同学*/
}

而且你应该很乐意去做(更少的迁移/数据库更新)。

我不喜欢在
TestModel
中使用
[ForeignKey]
属性。最好使用fluentapi来实现这一点。下面是在
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"));
    }
}
公共类测试模型
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟应用程序用户应用程序用户{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共虚拟TestModel TestModel{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
public ApplicationDbContext():基本(“DefaultConnection”)
{
}
公共DbSet TestModels{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity()
.has可选(m=>m.TestModel)
.WithRequired(m=>m.ApplicationUser)
.Map(p=>p.MapKey(“UserId”);
}
}

当scaffold使用Visual Studio 2015时,您可能会遇到错误CS1061,下面是一个很好的答案: /*************************/

将新模型添加到DbContext:

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

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

    /* rest of class */
}
public类ApplicationDbContext:IdentityDbContext
{
公共DbSet TestModels{get;set;}
/*其他同学*/
}
/*****************************/ 解决方案 删除控制器和受影响控制器的视图, 在DbContext中重命名为 像这样

public class ApplicationDbContext : IdentityDbContext<Users>
public类ApplicationDbContext:IdentityDbContext
重新启动VS,然后搭建脚手架


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

谢谢,这也起相反的作用,如果您希望AspNetUsers表引用具有外键的另一个表,请向ApplicationUser类添加相同的代码,更改相关类型。只要确保在另一个表中定义了键列。@如果我的实体在另一个类库项目中,而标识表在web项目中,如何添加外键关系?如果你有任何想法,请告诉我?