实体框架-无法将AspNetUsers中的关系添加到自定义表

NRKirby

在我的ASP.NET MVC应用程序中,我试图从AspNetUsers表到Recipes添加一对多关系

运行迁移后,存在从Recipes的关系AspNetUsers但没有从AspNetUsers到的关系Recipes

这就是我定义模型的方式:

public class ApplicationUser : IdentityUser
{
    public List<Recipe> Recipes { get; set; }

    <omitted for brevity>
}



public class Recipe 
{  
    <other properties omitted>

    public virtual ApplicationUser User { get; set; }
    public string UserId { get; set; }
}


public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Recipe> Recipes { get; set; }

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        Database.SetInitializer<ApplicationDbContext>(null);
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

我有:

  • 检查食谱表在SQL Management Studio中是否存在(确实如此)
  • 尝试使用Google搜索类似问题(我很可能在搜索错误)
  • Update-Database在控制台中运行
古卡斯

@NRKirby,我使用了与上面发布的代码完全相同的代码,这就是数据库中的代码:

在此处输入图片说明

我已经IdRecipe模型中添加了属性我不知道您是否在省略的属性中使用它。迁移的代码如下:

public partial class InitalCreate : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Recipes",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    UserId = c.String(maxLength: 128),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.AspNetUsers", t => t.UserId)
            .Index(t => t.UserId);

        CreateTable(
            "dbo.AspNetUsers",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    Email = c.String(maxLength: 256),
                    EmailConfirmed = c.Boolean(nullable: false),
                    PasswordHash = c.String(),
                    SecurityStamp = c.String(),
                    PhoneNumber = c.String(),
                    PhoneNumberConfirmed = c.Boolean(nullable: false),
                    TwoFactorEnabled = c.Boolean(nullable: false),
                    LockoutEndDateUtc = c.DateTime(),
                    LockoutEnabled = c.Boolean(nullable: false),
                    AccessFailedCount = c.Int(nullable: false),
                    UserName = c.String(nullable: false, maxLength: 256),
                })
            .PrimaryKey(t => t.Id)
            .Index(t => t.UserName, unique: true, name: "UserNameIndex");

        // Some other table definitions...
    }

    public override void Down()
    {
        // ...
        DropForeignKey("dbo.Recipes", "UserId", "dbo.AspNetUsers");            
        // ...
        DropIndex("dbo.AspNetUsers", "UserNameIndex");
        DropIndex("dbo.Recipes", new[] { "UserId" });

        // ...
        DropTable("dbo.AspNetUsers");
        DropTable("dbo.Recipes");
    }
}

如果数据库中的结果是您想要的,则我假设您至少在错误的其中一个命令上运行。

  • Enable-Migrations:在项目中启用代码优先迁移。
  • Add-Migration:为任何未决的模型
    更改提供迁移脚本
  • 更新数据库:将任何挂起的迁移应用于
    数据库。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章