1

我的项目分为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等)我得到错误:ClientDAOTransactionScopeDBDataContext

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.

4

1 回答 1

3

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.

于 2011-11-08T14:06:26.623 回答