已尝试附加或添加不是新的实体,可能是从另一个 DataContext 加载的。不支持此功能
我的项目分为PresentationLayer、BusinesLogicLayer和DataAccessLayer。每个创建的对象都会经过这些层。 简化来说:
SetFilter.xaml.cs
FilterFactory fFactory = new FilterFactory();
Filter myFilter = fFactory.Create(someClient, time, message);
FilterBLO filterBLO = new FilterBLO();
filterBLO.Save(myFilter);
FilterBLO.cs
FilterDAO filterDAO = new FilterDAO();
using (TransactionScope transcope = new TransactionScope())
{
filterDAO.Save(myFilter);
transcope.Complete()
}
FilterDAO.cs
using(DBDataContext dbdc = new DBDataContext)
{
dbdc.Filter.InsertOnSubmit(myFilter);
changeSet = dbdc.GetChangeSet();
dbdc.SubmitChanges()
}
Filter 使用包含 FilterID
和 的
。 (多对多关系)ClientFilter
表与表 Client
连接客户端ID
如果我创建新的 3 个对象一切正常,但如果我在数据库中获取现有的 Client
(也使用 ClientBLO
和 ClientDAO
所以在单独的 TransactionScope
和单独的 DBDataContext
中)我收到错误:
已尝试附加或添加非新实体, 也许是从另一个 DataContext 加载的。这不是 支持。
(我搜索了其他类似的线程,但没有找到问题的解决方案。)
最后是我的问题
如果Client
存在于数据库中,我应该如何保存myFilter
。我尝试 Attach()
将其附加到 FilterDAO.cs
中的数据上下文,但出现了相同的错误。
My project is divided to PresentationLayer, BusinesLogicLayer and DataAccessLayer. Every created object goes through these layers.
In simplifying:
SetFilter.xaml.cs
FilterFactory fFactory = new FilterFactory();
Filter myFilter = fFactory.Create(someClient, time, message);
FilterBLO filterBLO = new FilterBLO();
filterBLO.Save(myFilter);
FilterBLO.cs
FilterDAO filterDAO = new FilterDAO();
using (TransactionScope transcope = new TransactionScope())
{
filterDAO.Save(myFilter);
transcope.Complete()
}
FilterDAO.cs
using(DBDataContext dbdc = new DBDataContext)
{
dbdc.Filter.InsertOnSubmit(myFilter);
changeSet = dbdc.GetChangeSet();
dbdc.SubmitChanges()
}
Filter is connected with table Client
using ClientFilter
table containing FilterID
and ClientID
. (many-to-many relationship)
If I create new 3 objects everything's ok, but if I'm getting existing Client
in database (also using ClientBLO
and ClientDAO
so in separate TransactionScope
and separate DBDataContext
) I get error:
An attempt has been made to Attach or Add an entity that is not new,
perhaps having been loaded from another DataContext. This is not
supported.
(I searched other similar threads but I didn't found solution to my problem.)
And finally my question
How should I save myFilter
if Client
exists in database. I tried Attach()
it to datacontext in FilterDAO.cs
but I get the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用与 InsertOnSubmit 过滤器相同的 DataContext 从数据库获取 Client;那么您必须在 InsertOnSubmit 之前使用该对象在 Filter 对象中设置 Client 值。
You have to obtain Client from the database with the same DataContext you use to InsertOnSubmit the Filter; then you have to set Client value in the Filter object with that object before InsertOnSubmit.