映射模型实体框架4代码优先

时间:2012-03-15 00:35:37

标签: c# frameworks entity

有没有人可以帮我定义使用EF 4.3的关系映射。在尝试首先在代码中建模时,我有点迷失

逻辑在这里。

  1. 城市有1个或更多地点(例如1区,CBD,南,北)
  2. 地点有0个或更多场地
  3. 如果城市很小且没有任何位置(例如夏威夷)和1个所有者
  4. ,场地可能属于1个位置(例如纽约中央商务区或纽约南部)或属于1个城市
  5. 显示城市的场地数量。如果城市有位置,则显示否。所有地点的所有场地都属于城市。否则,只有场地的显示号属于该城市。
  6. 这是我的模特

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

1 个答案:

答案 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);