c# - Way to load navigation properties after saving object to database -
c# - Way to load navigation properties after saving object to database -
is there way 'bind' related properties after saving object database? example: html form post ticket object. has properties filled , foregin keys correct.
[httppost] [validateantiforgerytoken] public actionresult create(ticket ticket) { if (modelstate.isvalid) { db.tickets.add(ticket); db.savechanges(); //here want utilize ticket.responsibleemployee property homecoming redirecttoaction("index"); } homecoming view(ticket); } public class ticket { public int ticketid { get; set; } public string title { get; set; } [foreignkey("responsibleemployee")] public int responsibleemployeeid { get; set; } public virtual employee responsibleemployee { get; set; } } public class employee { public int employeeid { get; set; } public string lastname { get; set; } public string firstname { get; set; } public virtual ilist<ticket> tickets { get; set; } public employee() { this.tickets = new list<ticket>(); } }
is there simple way related properties? i've tried property 1 time again after save db.tickets.find(ticket.ticketid)
returned ticket doesn't have navigation properties assigned. tried db.entry(ticket).currentvalues.setvalues(ticket)
doesn't work.
i couldn't see info responsibleemployeeid here. assume it's part of object. can seek like:
[httppost] [validateantiforgerytoken] public actionresult create(ticket ticket) { if (modelstate.isvalid) { ticket.responsibleemployee = db.employees.find(responsibleemployeeid); db.tickets.add(ticket); db.savechanges(); homecoming redirecttoaction("index"); } homecoming view(ticket); }
c# asp.net-mvc entity-framework
Comments
Post a Comment