我有一个包含3个表的简单数据库模型:公司,类别和公司类别(这是一个只有2个FK的关系表: CompanyID和CategoryID)。
我的edmx模型它只显示公司和类别表(CompanyCategories以某种方式隐藏,因为它是一个简单的多对多关系表)。
在WCF服务中,我有一个GetDatabase()函数,它返回包含在一个大自定义对象中的所有数据库对象:
[OperationContract]
public FullDatabase GetDatabase()
{
DBEntities context = new DBEntities ();
FullDatabase mydb = new FullDatabase();
mydb.Companies = context.Companies.ToList();
mydb.Categories = context.Categories.ToList();
return mydb;
}
[OperationContract]
public FullDatabase UpdateDatabase(FullDatabase db)
{
// Here is my problem when removing a category from a company on
// the client its been brought back in my db object
}
class FullDatabase()
{
List<Company> Companies;
List<Category> Categories;
}
现在在客户端上,我使用GetDatabaseAsync()来检索_FullDB变量中的数据库。现在使用该变量我尝试了以下内容:
// Adding a category like that Works well
Company c = _FullDB.Companies.First();
c.Categories.Add(_FullDB.Categories.First());
wcfServiceClientObject.UpdateDatabaseASync(_FullDB);
.....
// Removing a category, doesn't work though :
Company c = _FullDB.Companies.First();
c.Categories.Remove(_FullDB.Categories.First());
wcfServiceClientObject.UpdateDatabaseASync(_FullDB);
// here my c.Categories.Count is updated correctly to delete the item
// but when on the server after (in the UpdateDatabase function) the item
// I deleted is still there
我真的不明白为什么Add会起作用而不是Remove。
答案 0 :(得分:0)
从公司中删除类别时,我还必须将该公司从类别中删除...
Company comp = _FullDB.Companies.First();
Category cat = _FullDB.Categories.First();
comp.Categories.Remove(cat);
cat.Companies.Remove(comp);
wcfServiceClientObject.UpdateDatabaseASync(_FullDB);