导航属性被配置为冲突的多重属性
本文关键字:属性 冲突 配置 导航 | 更新日期: 2023-09-27 18:05:34
嗨!我在两个表之间有一些关系问题,一对多。
整个概念如下:
表1:国家:除了它自己的字段之外,它还包含两个iccollections,其中一个指向地区集合,另一个指向城市集合。
表2:区域除了它自己的字段之外,它还包含一个CountryId和一个城市集合。
表3:城市除了它自己的字段之外,它还包含一个CountryId和一个RegionId(其中RegionId可以为空)。
其主要思想是,一个国家可以有地区或城市。这意味着从地区/城市到CountryId的每个外键不能为空。但是,从City到Region的外键允许为空,因为"一些"国家(在本例中是Region)是不必要的。
注意,这三个表还使用一个iccollection引用一个中间表,而中间表又存储它们之间的关系。
国家实体:
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
有人知道吗?很高兴得到任何帮助
其实我现在已经解决了。我在RegionMap中漏掉了这个:
很简单,但是当你长时间盯着你的代码时,这些东西很容易被忽略。
HasMany(t => t. cities).WithRequired(t => t. region);
应该是
HasMany(t => t. cities).WithOptional(t => t. region);
谢谢。
/J