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.
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
Post a Comment