asp.net mvc 4 - Implement CRUD for entity with foreign keys -


i'm trying implement little app simple crud operations in asp.net mvc4 entity framework. how should implement entity:

public class userprofile {     [key]     [databasegeneratedattribute(databasegeneratedoption.identity)]     public int userid { get; set; }     public string username { get; set; }      public virtual role userrole { get; set; }      public virtual employee employee { get; set; } } 

should make model this:

public class user {     public int userid { get; set; }      public string username { get; set; }      public int roleid { get; set; }      public int employeeid { get; set; } } 

and use add , edit views? or there better way it? can use userprofile class these views?

you have create 1 class implement crud.
in controllers (or better abstraction, in services layer) call dbcontext add, edit, delete or select entities.

take here: http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

here simple example add movie has category:

    [httppost]     public actionresult createmovie(string title, string category)     {         movie m = new movie();         m.title = title;         if (category != "" && category != null)             m.category = _context.categorys.first(c => c.id == int32.parse(category));         _context.movies.add(s);         _context.savechanges();          return redirecttoaction("index");     } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -