Entity Framework Code First Saving Multiple Records for the same primary/foreign key -
Text after "
The person has two equal tables, the difference is that each person has only one record in the PersonPreference table, but in the PersonRoles table There may be many. Both personality and personality are both primary and foreign key (Person_ID):
& lt; Table ("person") & gt; _ Public class person & lt; Key () & gt; _ Public property Person_ID as the name of the public property ObservableCollection (PersonRole) End String as the line of the public overridable property PersonPreference PersonPreference Public, as overridable property PersonRoles, Table ("PersonPreference") & gt ; _ Public class person confirmation & lt; Key () & gt; _ Public property person_id as an integer public property car string public property color as string and class & lt; Table ("Personroll") & gt; _ Public Class Personnel & Lieutenant; Key () & gt; _Public property Person_ID Integer public property role string ending class folk class inherited EntityTypeConfiguration (Person's) Public Sub New () Me.HasRequired (Function (T) t.Personference) .withRequiredPrincipal () Me.HasRequired (function form In the form of (t) t.PersonRoles) .withRequiredPrincipal () End Sub ending class There is this code as the saving role of the person playing roles (without adding the table) One way or need to do I have tried to make the overall key in the PersonRoles table but it did not work) modifications (PersonRoles is the primary key on the table)
This is not necessary for the role table but you need a composite key in the PersonRole table. I'm not familiar with VB.net so you can call it VB Can convert to net. Public class person {public id id (get; set;} public string name {get; set;} public virtual icon & lt; personroole & gt; person lines (get; set;) // Other properties} Public class PersonContext: DbContext {Protected Override Zero OnModelCreating (DbModelBuilder modelBuilder) {modelBuilder.Entity & lt; Person & gt; () HasMany (P = & gt; p.PersonRoles) .. Along with () HasForeignKey (r = & gt; R. P.C.I.D.); ModelBlider.Entity & Lt; Personnor & gt; (.) Husky (R = & gt; No Or (RPSID, R. Roll));} Public DbSet & lt; person & gt; person {get; set;}} Here is an example
var ctx = new PersonContext (); Var person = new person {name = "foo"}; person.psernolles = new list & lt; personRole & gt; () {role of new person {role = "A "}, New person {roll =" b "}} ctx.Persons.Add (person); ctx.SaveChanges ();
Comments
Post a Comment