Search code examples
asp.net-mvc-4entity-framework-6ef-code-firstentity-framework-migrations

Foreign Keys in another schema with Code-First creates referenced tables twice when adding migration


I will simplify my problem. I have 2 schemas: address and company. They each have their own context.

In the address schema I have these classes:

public class Country : GuidIdentifiable
    {
        public String Name { get; set; }
    }

public class City : GuidIdentifiable
    {
       public Guid Country_ID { get; set; }
       public String Name { get; set; }

       public Country Country { get; set; }
    }

In the company schema I have this class:

public class Store : GuidIdentifiable
{        
    public Guid Country_ID { get; set; }
    public Guid City_ID { get; set; }

    public String Name { get; set; }

    public Store Store { get; set; }
    public virtual Country Country { get; set; }
    public virtual City City { get; set; }
}

When I am adding migrations both address schema and company schema add Country and City tables. Address:

        CreateTable(
            "address.City",
            c => new
                {
                    ID = c.Guid(nullable: false),
                    Country_ID = c.Guid(nullable: false),
                    Name = c.String(nullable: false, maxLength: 255),
                    PTT = c.String(nullable: false, maxLength: 255),
                })
            .PrimaryKey(t => t.ID)
            .ForeignKey("address.Country", t => t.Country_ID, cascadeDelete: true)
            .Index(t => t.Country_ID);

        CreateTable(
            "address.Country",
            c => new
                {
                    ID = c.Guid(nullable: false),
                    Name = c.String(nullable: false, maxLength: 255),
                })
            .PrimaryKey(t => t.ID);

And company:

        CreateTable(
            "company.City",
            c => new
                {
                    ID = c.Guid(nullable: false),
                    Country_ID = c.Guid(nullable: false),
                    Name = c.String(nullable: false, maxLength: 255),
                    PTT = c.String(nullable: false, maxLength: 255),
                })
            .PrimaryKey(t => t.ID)
            .ForeignKey("company.Country", t => t.Country_ID, cascadeDelete: true)
            .Index(t => t.Country_ID);

        CreateTable(
            "company.Country",
            c => new
                {
                    ID = c.Guid(nullable: false),
                    Name = c.String(nullable: false, maxLength: 255),
                })
            .PrimaryKey(t => t.ID);
  • I have tried adding data annotation e.g.[Table(nameof(Country), Schema = "address")] to both of them, but they still appear in both migrations, just with the same schema prefix (address.) and then you can not update db with both migrations because it tells you those tables already exist.
  • I have tried using modelBuilder's Ignore() method to ignore City and Country just inside CompanyDbContext. That resulted in an error which says that FK is referencing something that doesn't exist.

How can I have a table in one schema reference a table in a different schema without the first schema creating copies of the referenced tables for itself?

=> I am fairly sure it can be done, because I have done it with database-first.


Solution

  • The problem here is not as much multiple schemas as multiple contexts is.

    The most usable solution I have found is this (#2).

    I have one context with the complete model which I use to maintain the DB and multiple smaller ones to use inside the application.