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.

  • Each font has a type. In windows when a font is removed the closest matching font will be used based on type and size. – jdweng Sep 04 '20 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. – Aluan Haddad Sep 04 '20 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();` – Cardi DeMonaco Jr Sep 04 '20 at 13:54

1 Answers1

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.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • 1
    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: https://stackoverflow.com/questions/55336035/is-it-possible-and-how-create-a-trigger-with-entity-framework-core may help but it's not a very elegant way to create the trigger so it might be better to modify your database directly. – Bugbeeb Sep 04 '20 at 14:59
  • 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. – TomTom Sep 04 '20 at 15:25