【问题标题】:Foreign Keys in another schema with Code-First creates referenced tables twice when adding migration在添加迁移时,另一个具有 Code-First 的架构中的外键会创建两次引用的表
【发布时间】:2019-12-04 14:43:21
【问题描述】:

我会简化我的问题。 我有 2 个架构:addresscompany。他们每个人都有自己的背景。

address 架构中,我有这些类:

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

company 架构中,我有这个类:

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

当我添加迁移时,address 架构和 company 架构都会添加 Country 和 City 表。 地址

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

公司

        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);
  • 我已经尝试将 data annotation eg[Table(nameof(Country), Schema = "address")] 添加到它们两个中,但它们仍然出现在两个迁移中,只是具有相同的架构前缀(地址。),然后您无法更新 db两次迁移,因为它告诉您这些表已经存在。
  • 我已尝试使用 modelBuilder 的 Ignore() 方法 忽略 CompanyDbContext 中的城市和国家/地区。这导致了一个错误,指出 FK 引用了不存在的东西。

如何让一个架构中的表引用不同架构中的表,而第一个架构不为其自身创建所引用表的副本?

=> 我相当肯定它可以完成,因为我已经用数据库优先做到了。

【问题讨论】:

    标签: asp.net-mvc-4 entity-framework-6 ef-code-first entity-framework-migrations


    【解决方案1】:

    这里的问题并不像多上下文那样多模式。

    我找到的最有用的解决方案是this (#2)。

    我有一个完整模型的上下文,我用来维护数据库和多个较小的模型在应用程序中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2020-06-27
      相关资源
      最近更新 更多