c# - Table organization using Entity Framework Core -


i trying organise tables in database using entity framework core.

roughly speaking, have following entities:

class book{   int id {get; set;}   list<chapter> chapters {get; set;} }  class chapter{   int id {get; set;}   list<page> pages {get; set;} }  class article{   int id {get; set;}   list<section> sections {get; set;} }  class section{   int id {get; set;}   list<page> pages {get; set;} }  class page{   int id {get; set;} } 

in class derives dbcontext create following tables:

dbset<book> book {get; set;}  dbset<article> article {get; set;}  dbset<section> section {get; set;}  dbset<chapter> chapter {get; set;} 

the problem cannot save records contain nonempty page collections database because each page resord gets 2 foreign keys - sectionid , chapterid, , when try put book record contains chapter records in turn contain page records database, complains sectionid cannot null page records.

what possible solutions apart creating 2 separate similar page entities, sectionpage , chapterpage?

update: in onmodelcreating(modelbuilder modelbuilder) method add following:

modelbuilder.entity<chapter>()              .hasmany(ch => ch.pages)              .withone()              .isrequired()        .ondelete(microsoft.entityframeworkcore.metadata.deletebehavior.cascade);  modelbuilder.entity<section>()              .hasmany(s => s.pages)              .withone()              .isrequired()        .ondelete(microsoft.entityframeworkcore.metadata.deletebehavior.cascade); 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -