i using entity framework in mvc web api , can't figure out why changes aren't being saved database. using following code:
[httppost] public httpresponsemessage postpxe(pxe pxe) { var pxerequestid = qsp in db.queue_server_provision .where(a => a.server_name == pxe.server_name) join isni in db.iaas_server_nic_information .where(a => a.mac == pxe.mac_address) on qsp.iaas_id equals isni.iaas_server_information_iaas_id select qsp.request_id; var qsp = db.queue_server_provision.find(pxerequestid.firstordefault()); db.queue_server_provision.attach(qsp); qsp.provision_status_code = "240.9"; db.entry(qsp).state = entitystate.modified; try { db.savechanges(); } catch (dbupdateconcurrencyexception e) { return request.createresponse(httpstatuscode.notfound, e.message); } catch (exception e) { return request.createresponse(httpstatuscode.notfound, "error"); } return request.createresponse(httpstatuscode.ok); } while debugging seems work fine until call savechanges() method, @ point provision_status_code value reverts before being modified.
the following sql generated entity framework:
exec sp_executesql n'update [dbo].[queue_server_provision] set [server_name] = @0, [environment_list_id] = @1, [operating_system_list_id] = @2, [container_size_list_id] = @3, [active_directory_list_id] = @4 ([request_id] = @5) select [iaas_id], [provision_status_code], [provision_status_text], [provision_phase], [last_update_time], [canceled] [dbo].[queue_server_provision] @@rowcount > 0 , [request_id] = 5', n'@0 nvarchar(16), @1 uniqueidentifier, @2 uniqueidentifier, @3 uniqueidentifier, @4 uniqueidentifier, @5 uniqueidentifier', @0=n'iaastest222', @1='31097372-e4a6-461d-afcc-bfaf069a6710', @2='cf44fe08-56de-4a7d-813a-08a7ad215e8b', @3='15aeb74f-e0cb-4219-ac69-8c623de8df46', @4='ea08bb7e-5c1f-4f6b-83d6-4bf9f6c55ff7', @5='34ca5f9c-2bf8-40e5-8bdd-5de7364c18c3'
this looks entity framework "thinks" provision_status_text column generated in database - either because it's identity (unlikely string column) or because it's computed column.
indication is:
the column not part of
setexpression inupdatestatement although changed column. if column database generated expected.the column returned in sql
selectexpression. database generated column expected again because ef wants update entity on client side current computed database values after insert or update.
the same applies way other columns occur in select statement.
if using code-first workflow check if properties annotated 1 of the
[databasegenerated(databasegeneratedoption.identity)] [databasegenerated(databasegeneratedoption.computed)] attributes or if
....hasdatabasegeneratedoption(databasegeneratedoption.identity) ....hasdatabasegeneratedoption(databasegeneratedoption.computed) is used fluent api.
if using database-first or model-first workflow edmx check edmx file storegeneratedpattern="identity" or storegeneratedpattern="computed" attributes. should find these settings in designer surface well.
it not mean column generated in database. if 1 of these settings in model metadata ef assume , behave way see it. mean database , model "out of sync" reason because manual changes have been done in database schema or model perhaps without updating other side.
Comments
Post a Comment