'Ef is creating new table when I added ForeignKey to another DbContext

I have two DbContext in my project, AppDbContext and MyNewApplicationDbContext.

I am using AppDbContext in multiple project that has table Keyword

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; }
}

Now in my new application I need to create table PoemKeyword in MyNewApplicationDbContext like below with a foreign key to Keyword table(that is in 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; }
}

then I added migrations but in new migrations It want to create new Keyword table but this table has already been added by 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);
                });

Note: MyNewApplicationDbContext And AppDbContext they are two in one database

I have now Common.Keyword table in my database why EF wants to create MyNewApplicationDbSchema.Keyword table too?

Edit

I recently changed the schema name of two diffrent DbContext but the problem not solved yet

        builder.HasDefaultSchema("SameSchemaNameInAllDbContext");


Solution 1:[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; }
    
}

Can u try to define like this. ET will recognize theese foreign key automaticly.
But dont forget the variables names should be same.
I mean if you define Poem as a Poem
then u should define PoemId as a foreign key.

My english is not great i hope this would help u

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sadullah DO?AN