使用WCF在实体框架中进行简单删除(多对多关系)

发布于 2024-11-08 05:22:06 字数 1605 浏览 0 评论 0原文

我有一个简单的数据库模型,包含 3 个表:CompaniesCategoriesCompanyCategories (这是一个只有 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

我真的不明白为什么“添加”可以工作,但“删除”却不行。

I have a simple database model containing 3 Tables : Companies, Categories and CompanyCategories (which is a relation table with only 2 FK : CompanyID and CategoryID).

My edmx model it only shows Companies and Categories tables (CompanyCategories is somehow hidded since its a simple many to many relationship table).

In the WCF service, I have a GetDatabase() function that returns all the database objets wrapped in one big custom object :

[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;
}

On the client now, I use GetDatabaseAsync() to retrieve the database in a _FullDB variable. Now using that variable I tried the following :

  // 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

I really dont understand why the Add would work but not the Remove.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜`诱少女 2024-11-15 05:22:06

终于发现问题了。现在它有效了,但我不确定这是最好的方法。

从公司中删除类别时,我还必须从类别中删除公司......

Company comp = _FullDB.Companies.First();
Category cat = _FullDB.Categories.First();

comp.Categories.Remove(cat);
cat.Companies.Remove(comp);

wcfServiceClientObject.UpdateDatabaseASync(_FullDB);

Finally found the problem. Now it works, but I'm not sure it is the best way to do it.

When removing the category from the company, I also had to also remove the company from the category...

Company comp = _FullDB.Companies.First();
Category cat = _FullDB.Categories.First();

comp.Categories.Remove(cat);
cat.Companies.Remove(comp);

wcfServiceClientObject.UpdateDatabaseASync(_FullDB);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文