如何克隆 POCO 实体并添加到上下文
我正在使用 EF4,并且我已经使用数据库结构中的代理创建了 POCO 对象。我有一个 POCO(对象),它与其他实体有很多关系。
我使用 DataContractSerializer 和 BinaryFormatter 创建了该对象的深层副本,并将其称为 clonedObject。
用于克隆的函数是:
public T CloneProxy<T>(T source)
{
var dcs = new System.Runtime.Serialization
.DataContractSerializer(typeof(T));
string filePath = "Initiative.txt";
using (FileStream file = new FileStream(filePath, FileMode.Create))
{
(new BinaryFormatter()).Serialize(file, source);
}
context.CreateProxyTypes(new Type[] { typeof(Initiative) });
using (FileStream file = new FileStream(filePath, FileMode.Open))
{
return (T)(new BinaryFormatter()).Deserialize(file);
}
}
现在我已经克隆了对象,如何将其添加到上下文中?我如何将它添加到数据库中?
我的对象(只是给你一个 POCO Initiative 的想法):
Initiative
{
InitI
InitName
<collection>Comments
}
Comments
{
CommentI
<FK>InitI
}
以下是我所做的一些方法和我收到的错误。
cloneInit.InitI = 0;
Data_Business.RQRMComment[] arr = new Data_Business.RQRMComment[1];
arr = cloneInit.RQRMComments.ToArray();
for (int x = 0; x < arr.Length; x++) //each (var x in cloneInit.RQRMComments)
{
RQRMComment thisC = arr[x];
int y = thisC.InitI;
thisC.InitI = 0;
thisC.ID = 0;
}
Context.AddObject("Initiatives", cloneInit);
Context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
错误:
ex = {“无法添加或附加该对象,因为其 EntityReference 的 EntityKey 属性值与该对象的 EntityKey 不匹配。”}
请帮忙,我在这方面花了太多时间。谢谢。
I am using EF4 and I have create POCO objects with proxies from my database structure . I have a POCO (object) which has lots of relationships to other entities.
I created a deep copy of the object using DataContractSerializer and BinaryFormatter and lets call it clonedObject.
function used for cloning is:
public T CloneProxy<T>(T source)
{
var dcs = new System.Runtime.Serialization
.DataContractSerializer(typeof(T));
string filePath = "Initiative.txt";
using (FileStream file = new FileStream(filePath, FileMode.Create))
{
(new BinaryFormatter()).Serialize(file, source);
}
context.CreateProxyTypes(new Type[] { typeof(Initiative) });
using (FileStream file = new FileStream(filePath, FileMode.Open))
{
return (T)(new BinaryFormatter()).Deserialize(file);
}
}
Now that I have clonedObject, how do I add it to the context? how do I add it to the database?
my object (just giving you an Idea of the POCO Initiative):
Initiative
{
InitI
InitName
<collection>Comments
}
Comments
{
CommentI
<FK>InitI
}
Here are some of the way I have done and errors I have received.
cloneInit.InitI = 0;
Data_Business.RQRMComment[] arr = new Data_Business.RQRMComment[1];
arr = cloneInit.RQRMComments.ToArray();
for (int x = 0; x < arr.Length; x++) //each (var x in cloneInit.RQRMComments)
{
RQRMComment thisC = arr[x];
int y = thisC.InitI;
thisC.InitI = 0;
thisC.ID = 0;
}
Context.AddObject("Initiatives", cloneInit);
Context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
Error:
ex = {"The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object."}
Please help, I have spent too much time on this. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要克隆我的实体,以便在表单上重新显示数据,以便用户可以选择“创建并添加类似”,以减少用户需要花费的精力为了将一系列类似的项目添加到我的数据库中。
我检查了一些选项,包括反射和序列化,但它们对于我想要实现的目标来说很混乱,然后我发现我可以克服“XYZ是对象关键信息的一部分并且无法修改”问题 - 即在插入后将我的实体主键设置为0(保存更改) ) - 使用以下代码:
I have had a need to clone my Entities for the purpose of re-displaying the data on a form so that a user can choose to "Create & Add Similar" in an effort to reduce the amount of effort a user needs to expend in order to add a range of similar items to my DB.
I checked out a few options including reflection & serialization but they are messy for what I am trying to achieve, I then discovered that I can overcome the "XYZ is part of the object's key information and cannot be modified" issue - i.e. set my entities primary key to 0 after insert (Save Changes) - with the following code: