please read the entity framework tutorials. you are going about this all wrong. on postback, using the key you should read the row into the context. then update the row with the postback values (TryUpdateModel), then db.SaveChanges();
bruce (sqlwork.com)
Marked as answer by eric2820 on Dec 03, 2012 12:43 AM
eric2820
Contributor
2777 Points
1161 Posts
How to Update a full record with Entity Framework?
Dec 01, 2012 09:10 PM|LINK
I have a situation where I need to update an entire records from two tables (Identity and Detials).
db.IdentlyData.Add( record ); gives an error relating to duplicate records cannot be inserted.
There is no Update method with Entity Framework so I'm trying to use a SqlStatement paramertized but it's not updating the database!
db.Details.SqlQuery( "Update [Details] set [firstName] = @firstName, [lastName] = @lastName, [address] = @address, [city] = @city, [state] = @state, " +
"[zipcode] = @zipcode, [phone] = @phone, [useFirstName] = @UseFirstName, [useLastName] = @useLastName, " +
"[useAddress] = @useAddress, [useCity] = @useCity, [useState] = @useState, [useZipcode] = @useZipcode, " +
"[usePhone] = @usePhone, [useEmail] = @useEmail Where [id] = @id",
new { data.MemberDetails.firstName, data.MemberDetails.lastName, data.MemberDetails.address, data.MemberDetails.city,
data.MemberDetails.state, data.MemberDetails.zipcode, data.MemberDetails.phone, data.MemberDetails.useFirstName,
data.MemberDetails.useLastName, data.MemberDetails.useAddress, data.MemberDetails.useCity, data.MemberDetails.useState,data.MemberDetails.useZipcode, data.MemberDetails.usePhone, data.MemberDetails.useEmail, data.MemberDetails.Id } );
What do I need to do to get this working?
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
DosNet
Member
117 Points
99 Posts
Re: How to Update a full record with Entity Framework?
Dec 02, 2012 10:59 AM|LINK
did you try UpdateModel ?
eric2820
Contributor
2777 Points
1161 Posts
Re: How to Update a full record with Entity Framework?
Dec 02, 2012 01:50 PM|LINK
What Namespace doe UpdateModel live within?
It's not an option with the namespaces I'm currently using.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
eric2820
Contributor
2777 Points
1161 Posts
Re: How to Update a full record with Entity Framework?
Dec 02, 2012 06:48 PM|LINK
UpdateModel is a protected internal method and it's description does not seem to do what I need it to do!
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
bruce (sqlwo...
All-Star
37626 Points
5574 Posts
Re: How to Update a full record with Entity Framework?
Dec 03, 2012 12:02 AM|LINK
please read the entity framework tutorials. you are going about this all wrong. on postback, using the key you should read the row into the context. then update the row with the postback values (TryUpdateModel), then db.SaveChanges();