c# - Entity Framework many to many - Code First - Migrations -


i have product class:

public class product { public int id { get; set; } public string name { get; set; } public virtual icollection<language> languages { get; set; } } 

language class:

public class language {     public int id { get; set; }     public string name { get; set; }     public virtual icollection<product> products { get; set; }  } 

in entitytypeconfiguration:

public class productmap : entitytypeconfiguration<product> {      public productmap()     {         haskey(m => m.id);         property(p => p.name).isrequired();         hasmany(p => p.languages)             .withmany(l => l.products)             .map(x => x.totable("produclanguages")                 .mapleftkey("productid")                 .maprightkey("languageid"));          //table           totable("products");     } } 

this creates third table expected, when executed update-database following seed:

protected override void seed(ecoomercecontext context)     {         var languages = new list<language>         {             new language {name = "portuguese"},             new language {name = "english"},             new language {name = "spanish"}         };          var languagept = new list<language>         {             new language {name = "portuguese"},         };          //languages.foreach(a => context.languages.add(a));          new list<product>         {           new product {name = "nameproduct1", languages = languages},           new product {name = nameproduct2 , languages = languagept},         }.foreach(a => context.products.add(a));          context.savechanges();     } 

it updates relationship table produclanguages this:

enter image description here

it inserting language not exist (number 4), result expect is:

enter image description here

what doing wrong ?

thanks in advance.

you need specify id of each entity, if pk identity. specifying id used in database crucial otherwise each update-database create new records.

also should use addorupdate extension method created seeding data during migrations.in method can specify expression know property should used check if it's necessary perform add or update operation, if don't specify id in entities, new row added , old 1 remain. @ end, seed method way:

 protected override void seed(consoleapplication3.yourcontext context)  {         var languages = new[]         {             new language {id = 1, name = "portuguese"},             new language {id = 2, name = "english"},             new language {id = 3, name = "spanish"},             new language {id = 4, name = "french"},         };          // way can add languages when there not associated product         // first parameter expression know properties should used when determining whether add or update operation should performed         context.languages.addorupdate(l => l.name, languages);          var products = new[]         {             new product {id = 1, name = "nameproduct1", languages = new list<language> {languages[0],languages[1],languages[3]}},             new product {id = 2, name = "nameproduct2", languages = new list<language> {languages[0]}},         };         context.products.addorupdate(p=>p.name,products);          context.savechanges(); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -