The world of code, originally thought the world relationship is very simple, sure way is the relationship everywhere. NET in the ORM framework in the EntityFramework as one of the leaders, greatly liberated the work of the duplication work, really improved a lot of productivity, but also encountered a lot of problems! Like the mapping of relationships!
Mapping of one-to-one relationships:
User account password Information table: Contains user name password mailbox and other account login information
public class Systemaccount {public systemaccount () { Id = Dateutils.generatednewguid (); } Public Guid Id {get; set;} public string Salt {get; set;} public string UserName {get; set;} public string Password {get; set;} public string Tele {get; set;} Public Guid UserId {get; set;} Public virtual user User {get; set;} Public virtual User CreateUser (string nick) { var user = new User (); User. Nick = Nick; return user; } }
User Information table: Contains the user's real name, latitude, nickname, gender and other member information
public class user {public user () { Id = Dateutils.generatednewguid (); } Public Guid Id {get; set;} public string Nick {get; set;} public int Gender {get; set;} Public Guid AccountId {get; set;} Public virtual Systemaccount Account {get; set;} }
Then the fluent API relationship is configured as follows
public class systemaccountmapping:farmerentitytypeconfiguration<systemaccount> { public Systemaccountmapping () {this . Haskey (r = r.id); This. Property (r = r.id). Hasdatabasegeneratedoption (databasegeneratedoption.none); This. Hasrequired (r = r.user). Withmany (). Hasforeignkey (FK = FK). UserId); } }
public class usermapping:farmerentitytypeconfiguration<user> {public usermapping () } {this . Haskey (r = r.id); This. Property (r = r.id). Hasdatabasegeneratedoption (databasegeneratedoption.none); This. Hasrequired (r = r.account). Withmany (). Hasforeignkey (FK = FK). AccountId); } }
Then add the database initialization seed data
Internal sealed class createifnoexistdatabase:dbmigrationsconfiguration<farmerobjectcontext> { Public Createifnoexistdatabase () { automaticmigrationsenabled = true; Automaticmigrationdatalossallowed = true; } protected override void Seed (Farmerobjectcontext context) { Systemaccount account = new Systemaccount (); Account. UserName = "shatan776"; Account. Salt = "oXML"; Account. Password = Stringutils.generatesaltedhash ("123456", Stringutils.createsalt (Appconstants.saltsize)); Account. User = account. CreateUser ("Lao Tzu is Zhang San"); Context. Set<systemaccount> (). ADD (account); Context. SaveChanges (); } }
Full face of the happy Debug, to a 16-year sprite, quietly waiting, the result of the taskbar universe first IDE VisualStudio plus Blue,
Constrain the FOREIGN KEY to ' fk_dbo. Farmer_user_dbo. Farmer_systemaccount_accountid ' Introducing table ' farmer_user ' may result in loops or multiple cascading paths. Please specify on DELETE no action or on UPDATE no action, or modify other FOREIGN KEY constraints.
Unable to create constraint. See the preceding error message.
Multiple cyclic references, Nani,
Is it that the user's navigation is loaded in the Systemaccount, and the user has a duplicate? So, comment out the navigation configuration in usermapping
This. Hasrequired (r = r.account). Withmany (). Hasforeignkey (FK = FK). ACCOUNTID);
Continue debug error basis, and then comment out the navigation ID
Public Guid AccountId {get; set;}
Da Da, finally generated a database, the seed data is also inserted
A one-to-one relationship on this generation, the hands of the son, with the son, Zhang San fancy John Doe ...
Pondering the previous account system moved, the previous primary key is an int type of database self-increment, modify the corresponding type, error, find Account_target, also, when inserted into the database when the primary key ID is not generated, so the user is not inserted, Stupid way, first insert account, and then insert user, temporarily solve the problem!
Look at that. Generated database foreign key graph
See database-generated relationships, 0 = "1 ... can send the Systemaccount entity class to generate the user, and the user generated Systemaccount will be an error, users information must have a member account record, and member account records can not have user information
More information reference
Entity Framework-clearing relationships-one-way, single-relationship based on foreign key associations
Entity Framework-A one-way relationship based on foreign key associations