Base solution for your next web application
Open Closed

How delete depended items from entity - entity framework #564


User avatar
0
klainer created

i have this entity Page:

public class Page : FullAuditedEntity<int, User>, IMultiLanguageEntity<PageTranslation>
{

    public string Name { get; set; }
    public string Content{ get; set; }

    public Page()
    {
        Translations = new List<PageTranslation>();
    }

    public virtual IList<PageTranslation> Translations { get; set; }
}

And entity PageTranslation:

[Table("PageTranslations")]
public class PageTranslation : Entity<int>, IEntityTranslation<Page>
{
    public Page Core { get; set; }
    public int CoreId { get; set; }
    public string Language { get; set; }

    public string Name { get; set; }
    public string Content{ get; set; }
}

I want to update page entity with updated values and tranlsations, so I call this service:

public void UpdatePage(UpdatePageInput input)
    {
        var item = _pageRepository.Get(input.Id);
        item.Content = input.Content;
        item.Description = input.Description;
        item.Title = input.Title;
        item.Name = input.Name;
        item.Translations.Clear(); // there is a problem
        item.Translations.addRange(input.Translations);
    }

When I call item.Translations.Clear() method I got this 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.

How to solve this in ABP ?

Thanks for help !


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Actually this is not an issue specific to ABP. This is completely related to EntityFramework. I can suggest two ways:

    1. Try cascade delete (delete on update). I'm not sure if this works.
    2. Change the collection manually. Add new items, remove deleted items and update changed items. So, I don't know a simple way of it. You can search it on web since it's related to EF. Please share your solution if you can find better :)
  • User Avatar
    0
    excelsior created

    use the compositekey like this

    [Table("PageTranslations")]
    

    public class PageTranslation : Entity<int>, IEntityTranslation<Page> { <span style="color:#FF0000"> [Key,Column(Order= 1)] //important or use FluentApi : HasKey(t=> new {t.Id,t.CoreId})</span> <span style="color:#FF0000">public int Id {get;set;} // id shuold be here</span> public Page Core { get; set; } <span style="color:#FF0000"> [Key,Column(Order= 1)]</span> public int CoreId { get; set; } public string Language { get; set; }

    public string Name { get; set; }
    public string Content{ get; set; }
    

    }

    The EntityFramework will delete Entry when the key is destory , so use the compositeKey is best way i think :D