Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Question on deleting many to many relationships. #2970


User avatar
0
codenewa created

In my application, I have two entities Attribute and Package which have a many to many relationship. So, I have a PacakgeAttribute entity which is a FullAuditedEntity

public class PackageAttribute: FullAuditedEntity{
...
}

public class Package: FullAuditedEntity{
  public IList<PackageAttribute> PackageAttributes{get;set;}
}

In this code, I cannot remove a PackageAttribute by doing:

package.Attribute.Remove(packageAttribute);
pacakgeRepository.UpdateAsync(package)

This throws error:

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.

However, I cannot make the relationship an identifying relationship, as it is a soft delete.

I tried including the Id as a part of the composite key but the database does not set the id column as an identity column. (Could this be an issue?)

So, I ended up using IRepository<PackageAttribute> to remove the packageAttribute by calling DeleteAsync() method.

Am I missing something? Or, is this how it is supposed to be done?


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes, in EF 6.x you need to do it like that. Or you can give Abp.GraphDiff package a try, <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1073">https://github.com/aspnetboilerplate/as ... ssues/1073</a>. It is developed by ABP community.

    Thanks.