【问题标题】:Ef is creating new table when I added ForeignKey to another DbContext当我将 ForeignKey 添加到另一个 DbContext 时,Ef 正在创建新表
【发布时间】:2020-10-19 11:58:27
【问题描述】:

我的项目中有两个DbContextAppDbContextMyNewApplicationDbContext

我在具有表Keyword 的多个项目中使用AppDbContext

public class AppDbContext : DbContext
{
    public virtual DbSet<Keyword> Keywords { get; set; }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.HasDefaultSchema("Common");
    }
}
public class Keyword
{
    public int Id { get; set; }
    public string Value { get; set; }
    public int ParentId { get; set; }
    [ForeignKey("ParentId")]
    public Keyword Parent { get; set; }

    public virtual ICollection<Keyword> Keywords { get; set; }
}

现在在我的新应用程序中,我需要在 MyNewApplicationDbContext 中创建表 PoemKeyword,如下所示,并使用 Keyword 表的外键(即在 AppDbContext 中)

public class MyNewApplicationDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.HasDefaultSchema("MyNewApplicationDbSchema");
        builder.Entity<PoemKeyword>()
                .HasKey(e => new { e.KeywordId, e.PoemId, e.PoetId });
        }
    public DbSet<PoemKeyword> PoemKeywords { get; set; }
}
public class PoemKeyword
{
    public int PoemId { get; set; }
    public int PoetId { get; set; }
    public int KeywordId { get; set; }

    [ForeignKey("PoemId")]
    public virtual Poem Poem { get; set; }
    [ForeignKey("KeywordId")]
    public virtual Keyword Keyword { get; set; }
    [ForeignKey("PoetId")]
    public virtual Poet Poet { get; set; }
}

然后我添加了迁移但在新的迁移中它想创建新的Keyword 表但该表已经被AppDbContext 添加了


            migrationBuilder.CreateTable(
                name: "Keyword",
                schema: "MyNewApplicationDbSchema",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Value = table.Column<string>(nullable: false),
                    ParentId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Keyword", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Keyword_Keyword_ParentId",
                        column: x => x.ParentId,
                        principalSchema: "MyNewApplicationDbSchema",
                        principalTable: "Keyword",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

注意:MyNewApplicationDbContextAppDbContext是二合一数据库

我的数据库中现在有 Common.Keyword 表,为什么 EF 也想创建 MyNewApplicationDbSchema.Keyword 表?

编辑

我最近更改了两个不同的DbContext 的架构名称,但问题还没有解决

        builder.HasDefaultSchema("SameSchemaNameInAllDbContext");

【问题讨论】:

  • 1 个数据库 + 多个 DbContexts + 迁移 == 问题。当 generationg 迁移时,EF Core 不知道目标数据库状态是什么。迁移是根据当前模型和上一个模型快照之间的差异生成的 - 两者都存储在迁移项目中。
  • @IvanStoev 两个 DbContext 都在一个迁移项目中
  • @IvanStoev 我从PoemKeyword 实体中删除了public virtual Keyword Keyword { get; set; } 现在EF 不添加关键字表。那么你知道是什么问题吗?

标签: c# asp.net-core entity-framework-core entity-framework-migrations


【解决方案1】:
public class PoemKeyword
{
    public int PoemId { get; set; }
    public int PoetId { get; set; }
    public int KeywordId { get; set; }

    public virtual Poem Poem { get; set; }
    public virtual Keyword Keyword { get; set; }
    public virtual Poet Poet { get; set; }
    
}

你能试着这样定义吗? ET 将自动识别这些外键。
但不要忘记变量名称应该相同。
我的意思是,如果您将 Poem 定义为 Poem
,那么您应该将 PoemId 定义为外键。

我的英文不好,希望对你有帮助

【讨论】:

  • 这不能回答问题。这与英语无关,只是没有解决手头的问题。
猜你喜欢
  • 2020-02-10
  • 1970-01-01
  • 2016-02-07
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
相关资源
最近更新 更多