SHARE
TWEET

Untitled

a guest Jan 16th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class City
  2. {
  3.     public City()
  4.     {
  5.         Attractions = new HashSet<Attraction>();
  6.     }
  7.     public int Id { get; set; }
  8.     public string Name { get; set; }
  9.     public string Description { get; set; }
  10.     public int Population { get; set; }
  11.     public int NumberOfAttractions { get { return Attractions.Count(); } }
  12.     public virtual ICollection<Attraction> Attractions { get; set; }
  13. }
  14.  
  15. public class Attraction
  16. {
  17.     public int Id { get; set; }
  18.     public string Name { get; set; }
  19.     public string Description { get; set; }
  20.     public int CityId { get; set; }
  21.     public virtual City City { get; set; }
  22. }
  23.  
  24. public class CityInfoContext : DbContext
  25. {
  26.     public CityInfoContext(DbContextOptions options) : base(options)
  27.     {
  28.     }
  29.  
  30.     public virtual DbSet<City> Cities { get; set; }
  31.     public virtual DbSet<Attraction> Attractions { get; set; }
  32.  
  33.     protected override void OnModelCreating(ModelBuilder modelBuilder)
  34.     {
  35.         modelBuilder.Entity<Attraction>(entity =>
  36.         {
  37.             entity.HasIndex(e => e.CityId);
  38.             entity.Property(e => e.Description).IsRequired().HasMaxLength(200);
  39.             entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
  40.             entity.HasOne(d => d.City).WithMany(p => p.Attractions).HasForeignKey(d => d.CityId);
  41.         });
  42.  
  43.         modelBuilder.Entity<City>(entity =>
  44.         {
  45.             entity.Property(e => e.Description).IsRequired().HasMaxLength(200);
  46.             entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
  47.         });
  48.     }
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top