有没有人可以帮我定义使用EF 4.3的关系映射。在尝试首先在代码中建模时,我有点迷失
逻辑在这里。
这是我的模特
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 :(得分:0)
如果您只是坚持在第1点和第2点中的陈述,您可能会发现它更简单。然后,一个小城市将有一个位置集合,但只有一个条目。
此外,您的地点还有一个场地集合,场地只属于业主和地点。
然后你有:
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);