整个概念如下:
表1:国家:
除了它自己的字段外,它还包含两个ICollections,其中一个指向一组区域,另一个指向城市。
表2:区域
除了它自己的领域,它还包含CountryId和ICollection of Cities。
表3:城市
除了它自己的字段外,它还包含CountryId和RegionId(其中regionId可以为空)。
主要思想是一个国家可以拥有地区或/和城市。
这意味着从region / city到CountryId的每个外键都不能为空。但是,来自City To Region的外键被允许为null,因为" some"在这种情况下,国家/地区是不必要的。
观察到所有三个表都使用ICollection引用中间表,而中间表又存储它们之间的关系。
国家/地区实体:
public class Country
{
public Guid Id { get; set; }
public int CountryId { get; set; }
public Guid ComponentId { get; set; }
public string NumCode { get; set; }
public string Code2 { get; set; }
public string Code3 { get; set; }
public virtual ICollection<Region> Regions { get; set; }
public virtual ICollection<City> Cities { get; set; }
public virtual ICollection<AdvertGeography> AdvertsGeographies { get; set; }
}
public class CountryMap()
{
// Primary Key
HasKey(t => t.Id);
Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Unique Index
Property(e => e.ComponentId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Unique_Component", 1) { IsUnique = true }));
Property(e => e.CountryId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Unique_Country", 1) { IsUnique = true }));
// Properties
// Table & Column Mappings
ToTable("Country");
Property(e => e.Id).HasColumnName("Id");
Property(e => e.CountryId).HasColumnName("CountryId");
Property(e => e.ComponentId).HasColumnName("ComponentId");
Property(e => e.Code2).HasColumnName("Code2");
Property(e => e.Code3).HasColumnName("Code3");
Property(e => e.NumCode).HasColumnName("NumCode");
// Relationships
HasMany(t => t.Regions)
.WithRequired(t => t.Country).WillCascadeOnDelete(false);
HasMany(t => t.Cities)
.WithRequired(t => t.Country).WillCascadeOnDelete(false);
}
地区实体:
public class Region
{
public Region()
{
this.Cities = new HashSet<City>();
}
public Guid Id { get; set; }
public Guid CountryId { get; set; }
public virtual Country Country { get; set; }
public string Name { get; set; }
public virtual ICollection<Office> Offices { get; set; }
public virtual ICollection<City> Cities { get; set; }
public virtual ICollection<OpenApplication> OpenApplications { get; set; }
public virtual ICollection<Subscription> Subscriptions { get; set; }
public virtual ICollection<AdvertGeography> AdvertsGeographies { get; set; }
}
public class RegionMap()
{
// Primary Key
HasKey(t => t.Id);
Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Unique Index
Property(e => e.Name)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Unique", 1) { IsUnique = true }));
Property(e => e.CountryId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Unique", 2) { IsUnique = true }));
// Properties
Property(e => e.Name).IsRequired().HasMaxLength(512);
// Table & Column Mappings
ToTable("Region");
Property(e => e.Id).HasColumnName("Id");
Property(e => e.Name).HasColumnName("Name");
Property(e => e.CountryId).HasColumnName("CountryId");
// Relationships
HasMany(t => t.Subscriptions)
.WithMany(t => t.Regions);
HasMany(t => t.OpenApplications)
.WithMany(t => t.Regions);
HasMany(t => t.Offices)
.WithRequired(t => t.Region);
HasMany(t => t.Cities)
.WithRequired(t => t.Region);
HasRequired(t => t.Country).WithMany(t => t.Regions).WillCascadeOnDelete(false);
}
城市实体:
public class City
{
public Guid Id { get; set; }
public Guid CountryId { get; set; }
public virtual Country Country { get; set; }
public Guid? RegionId { get; set; }
public virtual Region Region { get; set; }
public string Name { get; set; }
public virtual ICollection<AdvertGeography> AdvertsGeographies { get; set; }
}
public class CityMap()
{
//Primary Key
HasKey(e => e.Id);
Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Unique Index
Property(e => e.Name)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Unique", 1) { IsUnique = true }));
//Properties
Property(e => e.Name).IsRequired().HasMaxLength(512);
//Table & Column Mappings
ToTable("City");
Property(e => e.Id).HasColumnName("Id");
Property(e => e.Name).HasColumnName("Name");
Property(e => e.CountryId).HasColumnName("CountryId");
Property(e => e.RegionId).HasColumnName("RegionId");
// Relationships
HasRequired(t => t.Country).WithMany(t => t.Cities).WillCascadeOnDelete(false);
HasOptional(t => t.Region).WithMany(t => t.Cities).HasForeignKey(t => t.RegionId);
}
/J
有人有想法吗?很高兴得到任何帮助
祝你好运
答案 0 :(得分:3)
其实我现在已经解决了。我在RegionMap中错过了这个:
相当简单,但是当你盯着你的代码太久时,这些东西很容易滑倒。
HasMany(t =&gt; t.Cities) .WithRequired(t =&gt; t.Region);
应该是
HasMany(t =&gt; t.Cities) .WithOptional(t =&gt; t.Region);
非常感谢。
/ J