2012-03-15 10 views
1

EF 4.3を使用して関係マッピングを定義するのに役立つ人はいませんか。コードで最初にモデル化しようとすると少し失われますマッピングモデルエンティティフレームワーク4コード最初

ロジックはこちらです。

  1. は、A市は、1箇所以上(例えば1区、CBD、南、北)
  2. を持っている場所は、0以上の会場があり
  3. A会場5月1つの場所(例えばニューヨークCBDに属していますか、サウスニューヨーク州)、都市が小さく、場所がなく(ハワイなど)、所有者が1人の場合は1都市に属します。
  4. 市の会場数を表示します。都市に場所がある場合は、noを表示します。すべての場所のすべての会場のうち、都市に所属しています。そうでなければ、会場の表示はその市にのみ属します。ここで

は私のモデルは、市や会場、場所をマップしようとしたとき、私はそれらのモデルをマップしようとしましたが、非常に混乱

public class City 
{ 
    private ICollection<Location> _locations; 
    private ICollection<Venue> _venues; 

    public virtual int ID { get; set; } 
    public virtual string Name { get; set; } 

    public virtual ICollection<Location> Locations 
    { 
     get { return _locations ?? (_locations = new List<Location>()); } 
     protected set { _locations = value; } 
    } 

    public virtual ICollection<Venue> Venues 
    { 
     get { return _venues ?? (_venues = new List<Venue>()); } 
     protected set { _venues = value; } 
    } 
} 

public class Location 
{ 
    private ICollection<Venue> _venues; 

    public virtual int ID {get;set;} 
    public virtual string Name { get; set; } 

    public virtual int CityID{get;set;} 
    public virtual City City {get;set;} 

    public virtual ICollection<Venue> Venues 
    { 
     get { return _venues ?? (_venues = new List<Venue>()); } 
     protected set { _venues = value; } 
    } 
} 

public class Owner 
{ 
    public virtual int ID {get;set;} 
    public virtual string Name { get; set; } 

    public virtual int VenueID {get;set;} 
    public virtual Venue Venue {get;set;} 
} 

public class Venue 
{ 
    public virtual int ID {get;set;} 
    public virtual string Name { get; set; } 

    public virtual int LocationID {get;set;} 
    public virtual Location VenueLocation {get;set;} 

    public virtual int VenueCityID{get;set;} 
    public virtual City VenueCity {get;set;} 

    public virtual int VenueOwnerID{get;set;} 
    public virtual Owner VenueOwner {get;set;} 
} 

ある

public class Context : DbContext 
{ 
    public DbSet<City> City{ get; set; } 
    public DbSet<Locations> Locations{ get; set; } 
    public DbSet<Owner> Owners{ get; set; } 
    public DbSet<Venue> Venues{ get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Location>() 
      .HasRequired(loc => loc.City) 
       .WithMany(c => c.Locations) 
       .HasForeignKey(loc => loc.CityID); 

     modelBuilder.Entity<Venue>() 
      .HasRequired(v => v.VenueOwner) 
      .WithRequiredPrincipal(); 

     //confusing here when trying to map Location & City for this venue 
     modelBuilder.Entity<Venue>() 
      .HasRequired(v => v.Location) 
      .WithRequiredPrincipal(); 

     modelBuilder.Entity<Venue>() 
      .HasRequired(v => v.City) 
      .WithRequiredPrincipal(); 
    } 
} 

答えて

0

ます場合は、それが単純かもしれませんポイント1と2にあなたのステートメントを貼り付けるだけです。そして小さな都市にはロケーションコレクションがありますが、エントリは1つだけです。

また、あなたの場所には会場のcollectinoがあり、会場は所有者と場所にのみ所属します。

次にあなたが持っていると思います:

public class City 
{ 
    private ICollection<Location> _locations; 

    public virtual int ID { get; set; } 
    public virtual string Name { get; set; } 

    public virtual ICollection<Location> Locations 
    { 
     get { return _locations ?? (_locations = new List<Location>()); } 
     protected set { _locations = value; } 
    } 
} 

public class Location 
{ 
    private ICollection<Venue> _venues; 

    public virtual int ID {get;set;} 
    public virtual string Name { get; set; } 

    public virtual City City {get;set;} 

    public virtual ICollection<Venue> Venues 
    { 
     get { return _venues ?? (_venues = new List<Venue>()); } 
     protected set { _venues = value; } 
    } 
} 

public class Owner 
{ 
    public virtual int ID {get;set;} 
    public virtual string Name { get; set; } 

    public virtual int VenueID {get;set;} 
    public virtual Venue Venue {get;set;} 
} 

public class Venue 
{ 
    public virtual int ID {get;set;} 
    public virtual string Name { get; set; } 

    public virtual Location VenueLocation {get;set;} 

    public virtual Owner VenueOwner {get;set;} 
} 

を市内のすべての会場のためのクエリは、次にようになります。しかし場合は、市が唯一の場所のコレクションが含まれている場合、私はそれが簡単に知る

city.Locations.SelectMany(l => l.Venues); 
+0

私は小都市のすべての会場を手に入れたいと思います。ハワイ、どのように私は小さな都市には全く場所がないときにクエリを行うことができます –

+0

それは都市が少なくとも1つの場所を持っている必要がありますようにします。小さな都市には、その都市のすべての会場を含む単一の場所があります。この方法で、同じコードで大小の都市を処理することができます。 –

+0

都市に少なくとも1つの場所があるようにモデルをどのようにマップしますか?実際に私はまだこのアプローチを納得させていません。つまり、都市を定義しなければならず、次に都市の位置を定義する必要があります。 NSW州とAlbury地区のAlbury町? –

関連する問題