【发布时间】:2019-12-04 14:43:21
【问题描述】:
我会简化我的问题。 我有 2 个架构:address 和 company。他们每个人都有自己的背景。
在 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