asp.net mvc - MVC - Linking two Models using a foreign key -


how add foreign key linking club class , clubevent class in one-to-many relationship club parent class , clubevent child.

these models using

public class club     {         // class manage single club         [key]         public int clubid { get; set;      }//end club    public class clubevent     {         [key]         public int eventid { get; set; }        } 

i want connect these 2 tables can add events club can display relevant events each club

this pretty basic mvc, , if need stuff in future please complete tutorial: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application great!

since clubevent 'child' of club (meaning 1 club can have 1 or many events) want have reference parent in child. so, add reference clubevent linking each instance of object appropriate club. add clubevent object..

public int clubid {get; set;}  [foreignkey("clubid")] public virtual club club {get;set;} 

by adding navigation property (virtual club club) able make call parent object clubevent child. (club)this.clubevent.

displaying achieved querying clubevents table parent club id. like:

var events = db.clubevents.where(e => e.clubid == id) 

where 'id' clubid passed method in controller.

hope helps, if have issues check link out because has great examples every relation type in mvc.

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -