c# - Inserting a row into Relational database table -


i new c# , asp.net mvc. i'm building hr portal our company user can submit leave form among other things... i'm using mssql database server , using entity frame work communicate it. have 3 entities, user (containing user details), permissions (the user permissions allowing actions in app) , leave form table (where leave form details stored). there 1 many relationship between user - permission , 1 many relationship between user-leave. not fazed permissions gets created when user account being created.

the problem facing is, how add leave form specific user? below controller code:

[httppost] [validateantiforgerytoken] public actionresult leave(masterviewmodel model) {     docsubviewmodel mv = model.dsmodel;     int userid = convert.toint32(session["userid"]);     try     {         using (hrdcpdbcontainer db = new hrdcpdbcontainer())         {             var leave = db.leaves.create();             leave.datefrom = mv.datefrom;             leave.datesubmitted = datetime.now;             leave.dateto = mv.dateto;             leave.nrdays = mv.nrdays;             leave.reason = mv.specialleave;             leave.tlapproval = null;             leave.tlapprovaldate = null;             leave.tlapprovalid = mv.teamleaderid;             leave.dmapprovaldate = null;             leave.dmapprovalid = mv.depmanagerid;             leave.dmapproval = null;             leave.type = mv.type;             leave.user = userid;             db.leaves.add(leave);             db.savechanges();         }         viewbag.message = "leave form submitted successfully. redirected shortly...";         return view("result");     }     catch (exception ex)     {         viewbag.message = ex;         //viewbag.message = "leave form submitted successfully. redirected shortly...";         return view("result");     } 

the problem comes in leave.user = userid;. says:

cannot implicitly convert int portal.model.user

i can't seem find out how this...

you're telling put userid leave model asking user.

your relationship requires user go in there, you'll have update code little bit:

using (hrdcpdbcontainer db = new hrdcpdbcontainer()) {     var leave = db.leaves.create();     leave.user = db.users.first(x => x.id == userid); } 

this put reference actual user in new leave record. if go later , check out you'll see column in leave table called user_id has integer value in , set foreign key users table.

note error if no user exists having specified id value. if anticipate problem, rather use .firstordefault() instead of .first() , account value being null before add new leave object.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -