2016-09-05 30 views
-1

こんにちは!私は1対多の2つのテーブルの間にいくつかの関係する問題を持っています。

次のように全体のコンセプトは次のとおりです。
表1:国: それ自身のフィールドのほかに、それはまた、都市の地域の収集と他の1つのポイント2 ICollectionsが含まれています。

表2:地域 それ自身のフィールドのほかに、CountryIdとICollection of Citiesも含まれています。

表3:都市 それ自身のフィールドのほかに、CountryIdとRegionId(regionIdはnull可)を含んでいます。

主なアイデアは、国が地域または市を持つことができるということです。 これは、地域/都市からCountryIdまでの各外部キーをnullにすることができないことを意味します。しかし、City To Regionからの外部キーはnullになることが許されています。この場合、「一部の」国は不要なためです。

3つのテーブルはすべて、ICollectionを使用して、それらの間の関係を格納している中間テーブルにも参照していることに注意してください。ナビゲーションプロパティが競合する多重度で設定されていますFluent API

国エンティティ:

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


Anybody have an idea? Glad for any help 

Best regards 
/J 

答えて

0

実は私は今、それを解決しました。私はRegionMapでこれを忘れてしまった:

あなたはあまりにもあなたのコードを見詰めると、これらのものは簡単にスリップします。

HasMany(t => t.Cities) .WithRequired(t => t.Region);

hasManyの(T => t.Cities) .WithOptional(T => t.Region)すべきです。

ありがとうございます。

/j

関連する問題