1

I have the following relations configured.

Document (1------*) Fonts (1------*) Sections
    (1)                                 (*)
     +-----------------------------------+
  • A document has many fonts.
  • A document has many sections.
  • A font has many sections.

All operations are done via document.

Is there anyway I can achieve the following from Entity Framwork configuration or does EF has any other type of mechanism which I can use ?

If I delete a font from document, and if that font is referenced by a section, to have the section fontId default to some other font.

3
  • Each font has a type. In windows when a font is removed the closest matching font will be used based on type and size. Commented Sep 4, 2020 at 13:46
  • 1
    Whether you can do this at all, aside from EF, depends on your database. By the way, EF and EF-Core are different enough that it doesn't make sense to tag such a question with both. Commented Sep 4, 2020 at 13:47
  • Create another function that calls Delete. For example, function void DeleteFont(Font font) _context.Sections.Where(section.Font == font); [update to font considered default]; _context.Fonts.Delete(font); _context.SaveChanges(); Commented Sep 4, 2020 at 13:54

1 Answer 1

1

If I delete a font from document, and if that font is referenced by a section, to have the section fontId default to some other font.

Forget EF - not EF's place to easily do it.

I would have the font delete redirected with an INSTEAD OF trigger that then would set the default values and after this execute the delete.

So, it also works in pure SQL without thinking.

Sign up to request clarification or add additional context in comments.

2 Comments

I like thisanswer because a trigger would systematically handle the execution of the update and delete on the server side without having to build a function in EF core that you might forget to use on accident. To answer your question of how to accomplish this in EF Core, this: stackoverflow.com/questions/55336035/… may help but it's not a very elegant way to create the trigger so it might be better to modify your database directly.
Also never forget that you may want to do those changes without efcore - a trigger also works when you do something via ssms. And there is a BIG chance you WILL. At some point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.