我有许多表格,这些表格一起构成了某人可以采用的“测试”。测试人员可以采用多种类型(脚本)。我正在编辑一个Edit函数,它允许有人编辑Test Questions。我希望这些编辑后的问题能够显示在所有这种类型的新测试中,但在查看过去的测试分数时仍然会显示旧的问题。
为此,每个“测试”都有一个TestId
自动增量标识(及其名称)。在编辑测试时,我想用新的TestId
复制测试,并提出编辑问题。如何使用LINQ to SQL插入一行(使用新身份)的副本?
那么,使用LINQ to SQL来复制并将其插入到表中的最佳方法是什么?这:
DataContext db = new DataContext(ConnectionString);
//Copy old test into identical new test but with a different script ID
var oldScript = db.TestScripts.Single(ds => ds.TestScriptId == oldScriptID);
var newScript = oldScript;
db.TestScripts.InsertOnSubmit(newScript);
db.SubmitChanges();
当然
告诉我,我Cannot add an entity that already exists.
就是做这个要经过在TestScript每一行的唯一途径,并将其复制手动,然后将其和数据库将给它一个新的ID?
编辑
我也试过
DataContext db = new DataContext(ConnectionString);
//Copy old test into identical new test but with a different script ID
var oldScript = db.TestScripts.Single(ds => ds.TestScriptId == oldScriptID);
var newScript = new TestScript();
db.TestScripts.InsertOnSubmit(newScript);
db.SubmitChanges();
希望它将使一个新的空行,然后我可以newScript = oldScript
和提交的变化,但它给了我SqlDateTime overflow
错误。
Hrm。这给了我一个“包含周期并且不能被序列化”的错误。我的TestScript实体指向许多指向许多TestScriptItemOption实体的TestScriptItem实体。 – Marcus 2011-02-16 21:36:53