asp.net mvc - Structuring A Linq Query -


i've searched site , googled no avail. perhaps i'm tired , hence googling wrong thing, don't know. i'm wits end , use or advice. i'm building touring site using asp.net mvc 4, , i've encountered problem correct query use information need database.

i have class safari stores information particular safari. have class highlight stores information highlights of particular safari. such, safari can have several highlights in one-to-many relationship. had highlights field in safari of type list, still had trouble seeding , querying details "highlights" , field "highlights" not being created in safari table, made highlights class. still wasn't able seed fields highlight, since table created, able manually (so know table has data).

i want able query database not availabe safaris, related highlights. i've included classes safari, highlight, safariscontroller , configuration.

safari.cs

    public int id { get; set; }     public string name { get; set; }     public string description { get; set; }     public string zone { get; set; }     public string location { get; set; }     public int cost { get; set; }     public list<highlight> highlights { get; set;  

highlight.cs

    public int id { get; set; }     public string content { get; set; }     public int safariid { get; set; } 

safariscontroller.cs

    private tourstraveldb db = new tourstraveldb();     public actionresult index()     {         var model = db.safaris.tolist();         return view(model);     } 

configuration.cs

    protected override void seed(travel_site.models.tourstraveldb context)     {         context.safaris.addorupdate(             r => r.name,             new safari              {                  name = "tsavo east safari",                  description = "this short kenya safari tour",                  location = "tsavo east national park",                  cost = 1200,                  zone = "nairobi",                 highlights = new list<string>(new string{                     "tsavo east national park",                      "views of local homesteads on safari route.",                      "scenic tsavo east savannah plains.",                      "elephants, giraffe, zebra, buffalo, cheetah, lions etc.",                      "panoramic views of tsavo east  during  lunch stop."                 })             },             new safari              {                  name = "amboseli overnight safari",                  description = "camp @ amboseli national park. ",                  location = "amboseli national park",                  cost = 1200,                  zone = "nairobi",                  highlights = new list<string>(new string{                     "amboseli national park",                      "hippopotatamus",                      "flamingoes"                 })             }         );     } 

i appreciate or pointers in right direction. cheers

you need reference safari , not id in highlight class. in experience easier debug such relationships , seed content if separate them during development.

safari.cs

[key] public int safariid { get; set; } public string name { get; set; } public string description { get; set; } public string zone { get; set; } public string location { get; set; } public int cost { get; set; } public list<highlight> highlights { get; set;  } 

highlight.cs

[key] public int id { get; set; } public string content { get; set; } [foreignkey("safari")] public int? safariid { get; set; } [foreignkey("safariid")] public virtual safari safari { get; set; } 

configuration.cs

var safaris = new list<safari>     {         new safari         {             name = "tsavo east safari",              description = "this short kenya safari tour",              location = "tsavo east national park",              cost = 1200,              zone = "nairobi"         },         new safari          {              name = "amboseli overnight safari",              description = "camp @ amboseli national park. ",              location = "amboseli national park",              cost = 1200         }     }; safaris.foreach(i => context.safaris.add(i)); context.savechanges();  var highlights = new list<highlight>     {         new highlight         {             content = "tsavo east national park",             safari = (from s in context.safaris name == "tsavo east safari" select e)         }     }; highlights.foreach(i => context.highlights.add(i)); context.savechanges(); 

i hope helps you...


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -