Updating a record using a class object with Entity Framework in C# -
i have entity class includes many fields. make simple, let's assume class defined following:
public partial class branches { public int num { get; set; } public string name { get; set; } public string street { get; set; } public string city { get; set; }
in api class, want define function updating record of type. creating function update each field separately seems quite overhead application, decided define 1 function updates fields. can assign object received function directly object used update following?
void updatebranch(branches branch) { using (var dbcontext = new entitiescontext()) { var result = dbcontext.branches.singleordefault(b => b.num == branch.num); if (result != null) { **result = branch;** dbcontext.savechanges(); } } }
i'm using entity framework version 6.0
yes can use it, following code
void updatebranch(branches branch) { using (var dbcontext = new entitiescontext()) { dbcontext.branches.attach(branch); dbcontext.entry(branch).state = entitystate.modified; dbcontext.savechanges(); } }
when attach entity dbcontext, entity framework aware instance exist in database , perform update operation.
Comments
Post a Comment