c# - Entity Framework Code First - strange behaviour with circular relationship -


i'm struggling understand how code first sets relationships based on model. here's model:

public class person {     [key]     public string personname { get; set; }      [required]     public virtual nation nationofbirth { get; set; }      [required]     public virtual nation currentnationofresidence { get; set; } }  public class nation {     [key]     public string nationname { get; set; }      public virtual person currentsecondincommand { get; set; } } 

at first got error while creating database, able solve adding couple of modelbuilder commands:

modelbuilder.entity<person>()     .hasrequired(p => p.nationofbirth)     .withrequireddependent()     .willcascadeondelete(false); modelbuilder.entity<person>()     .hasrequired(p => p.currentnationofresidence)     .withrequireddependent()     .willcascadeondelete(false); 

and added:

modelbuilder.entity<nation>()     .hasoptional(n => n.currentsecondincommand)     .withoptionaldependent()     .willcascadeondelete(false); 

now when try , populate database, following code:

var america = new nation { nationname = "america" }; context.nations.add(america); var bush = new person { personname = "bush", currentnationofresidence = america, nationofbirth = america }; var obama = new person { personname = "obama", currentnationofresidence = america, nationofbirth = america }; var biden = new person { personname = "biden", currentnationofresidence = america, nationofbirth = america }; context.people.add(bush); context.people.add(obama); context.people.add(biden); context.savechanges(); 

at point @ 'bush' added, both currentnationofresidence , nationofbirth properties not null him , set 'america' object. 'obama' added, currentnationofresidence , nationofbirth properties on 'bush' both become null. , same happens 'obama' once 'biden' added.

if inspect database generated this, can't see foreign key columns created on people table, have expected code first have automatically generated 2 foreign keys 2 nation fields on person (that's thought first 2 modelbuilder commands - description of "withrequireddepedent"). , more bizarrely there single person called "america" has been added database, nation called america.

i'd appreciate advice in general on model, if can provide it. realise have circular relationship here, have thought ok because nations aren't required have currentsecondincommand. realise it's bit complex (or perhaps convoluted better word) think makes sense:

  • a person must have single (nation) nationofbirth.
  • a person must have single (nation) currentnationofresidence.
  • a nation can optionally have single (person) currentsecondincommand.

any advice welcome, thanks.

the 2 relations person nation aren't 1 one, many one:

  • a person must have single (nation) nationofbirth. nation has many persons born there
  • a person must have single (nation) currentnationofresidence. nation has many residents

so configuration should be:

modelbuilder.entity<person>()     .hasrequired(p => p.nationofbirth)     .withmany()     .willcascadeondelete(false); modelbuilder.entity<person>()     .hasrequired(p => p.currentnationofresidence)     .withmany()     .willcascadeondelete(false); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -