Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework: Incorrect data while using Many-to-many relationship
    primarykey
    data
    text
    <p>I have following C# code that uses Entity Framework <strong>Code First</strong> approach. The tables are created in database; but the data entered is incorrect.</p> <blockquote> <p>Person 1 is member of Club 1 and Club 3.</p> <p>Person 2 is member of Club 2 and Club 3</p> <p>That means Club 2 has only one member.</p> </blockquote> <p>But using the following query it can be seen that the data reached in database is incorrect.</p> <p>What change need to be done in C# code in order to make it correct?</p> <p><img src="https://i.stack.imgur.com/8fblx.jpg" alt="enter image description here"></p> <pre><code> static void Main(string[] args) { Database.SetInitializer&lt;NerdDinners&gt;(new MyInitializer()); string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30"; using (var db = new NerdDinners(connectionstring)) { Club club1 = new Club(); Club club2 = new Club(); Club club3 = new Club(); Person p1 = new Person(); Person p2 = new Person(); List&lt;Club&gt; clubsForPerson1 = new List&lt;Club&gt;(); clubsForPerson1.Add(club1); clubsForPerson1.Add(club3); List&lt;Club&gt; clubsForPerson2 = new List&lt;Club&gt;(); clubsForPerson2.Add(club2); clubsForPerson2.Add(club3); List&lt;Person&gt; personInClub1 = new List&lt;Person&gt;(); personInClub1.Add(p1); List&lt;Person&gt; personInClub2 = new List&lt;Person&gt;(); personInClub2.Add(p2); List&lt;Person&gt; personInClub3 = new List&lt;Person&gt;(); personInClub3.Add(p1); personInClub3.Add(p2); club1.Members=personInClub1; club2.Members=personInClub2; club3.Members=personInClub3; p1.Clubs = clubsForPerson1; p2.Clubs = clubsForPerson2; db.Clubs.Add(club1); db.Clubs.Add(club2); db.Clubs.Add(club3); db.Persons.Add(p1); db.Persons.Add(p2); int recordsAffected = db.SaveChanges(); } } namespace LijosEF { public class Person { public int PersonId { get; set; } public virtual ICollection&lt;Club&gt; Clubs { get; set; } } public class Club { public int ClubId { get; set; } public virtual ICollection&lt;Person&gt; Members { get; set; } } public abstract class PaymentComponent { public int PaymentComponentID { get; set; } public int MyValue { get; set; } public abstract int GetEffectiveValue(); } public partial class GiftCouponPayment : PaymentComponent { public override int GetEffectiveValue() { if (MyValue &lt; 2000) { return 0; } return MyValue; } } public partial class ClubCardPayment : PaymentComponent { public override int GetEffectiveValue() { return MyValue; } } public partial class Payment { public int PaymentID { get; set; } public List&lt;PaymentComponent&gt; PaymentComponents { get; set; } public DateTime PayedTime { get; set; } } public class MyInitializer : CreateDatabaseIfNotExists&lt;NerdDinners&gt; { //Only one identity column can be created per table. protected override void Seed(NerdDinners context) { } } //System.Data.Entity.DbContext is from EntityFramework.dll public class NerdDinners : System.Data.Entity.DbContext { public NerdDinners(string connString): base(connString) { } protected override void OnModelCreating(DbModelBuilder modelbuilder) { //Fluent API - Plural Removal modelbuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); } public DbSet&lt;Person&gt; Persons { get; set; } public DbSet&lt;Club&gt; Clubs { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload