我尝试使用以下代码使用Entity Framework插入新记录。在BuildRequest类的BuildRequest.Save()方法中,当' Insert'发生,并且db.Save()完成后,为BuildRequest正确生成了JobId(表示正在正确完成Insert),但是记录没有添加到数据库中。
当我签入SQL分析器时,这是插件尝试执行的操作:
exec sp_executesql N'INSERT [dbo].[BuildQueue]([ApplicationId], [BuildReason])
VALUES (@0, @1)
SELECT [JobId]
FROM [dbo].[BuildQueue]
WHERE @@ROWCOUNT > 0 AND [JobId] = scope_identity()',N'@0 int,@1 tinyint,@0=5819,@1=0
我打电话来创建一个新的工作':
BuildRequest job = new BuildRequest(ApplicationId, Core.BuildReasons.NewApp);
job.Save()
使用以下类:
public class BuildRequest
{
private Data.BuildQueue _buildRequest;
public BuildRequest(int applicationId, BuildReasons reason)
{
_buildRequest = new Data.BuildQueue();
ApplicationId = applicationId;
BuildReason = reason;
}
public int JobId
{
get { return _buildRequest.JobId; }
}
public int ApplicationId
{
get
{
return _buildRequest.ApplicationId;
}
set
{
_buildRequest.ApplicationId = value;
}
}
public BuildReasons BuildReason
{
get
{
return (BuildReasons)_buildRequest.BuildReason;
}
set
{
_buildRequest.BuildReason = (byte)value;
}
}
public int Save()
{
using (Core.Data.UnitOfWork db = new Data.UnitOfWork())
{
Data.BuildQueue buildRequest = db.BuildQueueRepository.Get(a => a.JobId == this.JobId).SingleOrDefault();
if (buildRequest == null)//current build request doesn't already exist
{
db.BuildQueueRepository.Insert(_buildRequest);
}
else // we're editing an existing build request
{
db.BuildQueueRepository.Update(_buildRequest);
}
db.Save();
// **At this point, _buildRequest has a JobId, but no record is added to the database
return JobId;
}
}
}
到目前为止我检查过的内容:
可能会发生什么?正确运行的代码有什么不同?
答案 0 :(得分:0)
卫生署!如果有新记录处于特定状态,则会将新记录从队列中取出。事实证明,这个特殊的例程立即设置了该状态,并且正在输入记录,然后立即移动到另一个表。