C# LINQ EF复制并粘贴带有差异字段的记录
我需要复制并粘贴我的表中的一些记录,只需更改一个字段。 这是我的密码:C# LINQ EF复制并粘贴带有差异字段的记录,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,我需要复制并粘贴我的表中的一些记录,只需更改一个字段。 这是我的密码: using (ClearWhiteDBEntities cwContext = new ClearWhiteDBEntities()) { var qlstfld = from lstflds in cwContext.tblListFields where lstflds.li
using (ClearWhiteDBEntities cwContext = new ClearWhiteDBEntities())
{
var qlstfld = from lstflds in cwContext.tblListFields
where lstflds.listId == theLongSrc
select lstflds;
foreach (var item in qlstfld)
{
tblListField lstFldRow = new tblListField
{
name = item.name,
filterFieldId = item.filterFieldId,
listId = theLongDes, //this field must be change in paste
continueById = item.continueById,
destinationId = item.destinationId,
conditionId = item.conditionId,
userId = userId,
date = Convert.ToDateTime(DateTime.Now.ToShortDateString()),
time = DateTime.Now.TimeOfDay,
IP = trueIp
};
cwContext.AddTotblListFields(lstFldRow);
cwContext.SaveChanges();
}
}
但我得到了这个错误:
在提供程序连接上启动事务时出错。有关详细信息,请参见内部异常
复制和粘贴记录但只更改一个字段的最佳解决方案是什么?如果使用更改跟踪:
using (ClearWhiteDBEntities cwContext = new ClearWhiteDBEntities())
{
var qlstfld = from lstflds in cwContext.tblListFields
where lstflds.listId == theLongSrc
select lstflds;
foreach (var item in qlstfld)
{
cwContext.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Added);
item.Id = 0;
item.listId = theLongDes; //this field must be change in paste
}
cwContext.SaveChanges();
}
不,我插入了一条与其他记录相同的新记录,但使用了差异字段,在这里我需要添加一条新记录。我需要读取一些记录并使用字段差异克隆它们。让我检查一下。谢谢您的支持,它可以工作,请您指示我阅读更多有关此操作的信息,我以前从未见过此操作。再次感谢。我希望能帮助你