我在 MVC Web API 中使用实体框架,但我无法弄清楚为什么没有将更改保存到数据库中。我正在使用以下代码:
[HttpPost]
public HttpResponseMessage PostPXE(PXE pxe)
{
var pxeRequestID =
from 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);
}
虽然在我调用 SaveChanges() 方法之前调试一切似乎都可以正常工作,但此时 Provision_Status_Code 值恢复为修改前的值。
以下 SQL 是从实体框架生成的:
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
where ([Request_ID] = @5)
select [IaaS_ID],
[Provision_Status_Code],
[Provision_Status_Text],
[Provision_Phase],
[Last_Update_Time],
[Canceled]
from [dbo].[Queue_Server_Provision]
where @@ROWCOUNT > 0 and [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'