我有一个表单,允许用户更新并创建新的数据库条目,我能够更新但不能创建。以下是dal
if (actionrequired == Crud.Modify)
{
t_ProvisionSetup setupToBeChanged = (from p in Entities.t_ProvisionSetup
where p.ProvisionSetupId == provision.ProvisionSetupId
select p).Single();
setupToBeChanged.ModifiedOn = DateTime.UtcNow;
setupToBeChanged.ModifiedBy = userId;
setupToBeChanged = MapSetupToEntity(provision, setupToBeChanged);
Entities.SaveChanges();
setupid = setupToBeChanged.ProvisionSetupId;
}
if (actionrequired == Crud.Add)
{
t_ProvisionSetup setupToBeChanged = new t_ProvisionSetup();
setupToBeChanged = MapSetupToEntity(provision, setupToBeChanged);
setupToBeChanged.ModifiedBy = userId;
setupToBeChanged.ModifiedOn = DateTime.UtcNow;
setupToBeChanged.CreatedBy = userId;
setupToBeChanged.CreatedOn = DateTime.UtcNow;
Entities.SaveChanges();
setupid = setupToBeChanged.ProvisionSetupId;
}
它运行正常没有错误,但不会创建新记录。
答案 0 :(得分:2)
可能你需要这样的东西?
Entities.t_ProvisionSetup.Add(setupToBeChanged);
在调用Entities.SaveChanges()。
之前