我的项目分为PresentationLayer、BusinesLogicLayer和DataAccessLayer。每个创建的对象都会经过这些层。在简化:
SetFilter.xaml.cs
FilterFactory fFactory = new FilterFactory();
Filter myFilter = fFactory.Create(someClient, time, message);
FilterBLO filterBLO = new FilterBLO();
filterBLO.Save(myFilter);
过滤器BLO.cs
FilterDAO filterDAO = new FilterDAO();
using (TransactionScope transcope = new TransactionScope())
{
filterDAO.Save(myFilter);
transcope.Complete()
}
过滤器DAO.cs
using(DBDataContext dbdc = new DBDataContext)
{
dbdc.Filter.InsertOnSubmit(myFilter);
changeSet = dbdc.GetChangeSet();
dbdc.SubmitChanges()
}
过滤器Client
使用ClientFilter
包含FilterID
和的表与表连接ClientID
。(多对多关系)
如果我创建新的 3 个对象,一切正常,但如果我Client
在数据库中存在(也在单独和单独中使用ClientBLO
等)我得到错误:ClientDAO
TransactionScope
DBDataContext
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.