0

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 - http://www.aspnetboilerplate.com/?

Thanks for help !

Martin Haščák
  • 350
  • 6
  • 29

2 Answers2

0

While assuming u have a DBContext:

U can declare the on delete action:

protected override void OnModelCreating(DbModelBuilder modelBuilder){

    modelBuilder.Entity<Page>()
        .HasOptional(a => a.Translations)
        .WithMany()
        .WillCascadeOnDelete(true);
}
vwseppe
  • 36
  • 1
  • 7
  • When create your fluent api setting and start add-migrate action give me this result: **RenameColumn(table: "dbo.Pages", name: "CoreId", newName: "Translations_Id"); CreateIndex("dbo.Pages","Translations_Id"); }** which is nonsense i think – Martin Haščák Dec 07 '15 at 10:53
0

did you try

_pageRepository.DeleteAll();

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55