0

I have master table and details table. For example master table is ObjectSet<'MasterObject> and details table is ObjectSet<'DetailObject>. So each MasterObject contains EntityCollection<'DetailObject>. As I understand I can remove DetailObject from database using following:

  1. EntityCollection<'DetailObject> ec = masterObject.DetailObjects; // as navigation property
  2. ec.Remove(deleting_detail_object); // deleting_detail_object will be removed and marked for deleting.
  3. context.SaveChanges(); // I have exception

After Remove() the deleting_detail_object.MasterObject (navigation property) is null. It is normal. But context.SaveChanges() give me following exception :

"The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted."

I can delete this DetailObject using context.RemoveObject(), but is it possible to do it using EntityCollection<>?

4

1 回答 1

0

我认为这个问题与 EntityCollection 无关,我认为您的数据库架构有问题。如果您的主表具有不可为空的详细信息的 FK,则您无法删除详细信息表行,因为这会违反您的数据库架构。

要么更改它以使 FK 可以为空,要么重新设计您的数据库架构。

如果这是您需要支持的操作,也许您的详细信息表中应该有一个 FK 引用您的主表。因此,您可以删除详细信息行。

如果这没有帮助,请提供一个脚本来说明您如何创建数据库表。

于 2013-05-07T12:36:55.077 回答