Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8321965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:12:31+00:00 2026-06-08T23:12:31+00:00

I have following C# code that uses Entity Framework Code First approach. The tables

  • 0

I have following C# code that uses Entity Framework Code First approach. The tables are created in database; but the data entered is incorrect.

Person 1 is member of Club 1 and Club 3.

Person 2 is member of Club 2 and Club 3

That means Club 2 has only one member.

But using the following query it can be seen that the data reached in database is incorrect.

What change need to be done in C# code in order to make it correct?

enter image description here

    static void Main(string[] args)
    {

        Database.SetInitializer<NerdDinners>(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<Club> clubsForPerson1 = new List<Club>();
            clubsForPerson1.Add(club1);
            clubsForPerson1.Add(club3);

            List<Club> clubsForPerson2 = new List<Club>();
            clubsForPerson2.Add(club2);
            clubsForPerson2.Add(club3);
                            
            List<Person> personInClub1 = new List<Person>();
            personInClub1.Add(p1);
            
            List<Person> personInClub2 = new List<Person>();
            personInClub2.Add(p2);

            List<Person> personInClub3 = new List<Person>();
            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<Club> Clubs { get; set; }
}

public class Club
{
    public int ClubId { get; set; }
    public virtual ICollection<Person> 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 < 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<PaymentComponent> PaymentComponents { get; set; }
    public DateTime PayedTime { get; set; }

}


public class MyInitializer : CreateDatabaseIfNotExists<NerdDinners>
{
    //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<PluralizingTableNameConvention>();
    }

    public DbSet<Person> Persons { get; set; }
    public DbSet<Club> Clubs { get; set; }
    

    
    
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:12:34+00:00Added an answer on June 8, 2026 at 11:12 pm

    There was no problem actually. I thought about it as a problem due to the order of id creation. I have got some other issues that I have posted in Entity Framework: Duplicate Records in Many-to-Many relationship.

    It uses

    ((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)entity); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following code that retrieves data from the database and then
I have the following code that uses Sequence objects to read data from a
I have the following code, that uses session but i have an error in
I have been using Entity Framework CTP with Code-First as in this tutorial by
I have the following code that uses a for loop and I would like
Ok, I have the following code that used to work but now it does
I have the following C# code that uses DLLImport. using System; namespace LvFpga {
I have an entity model build using EF4.1 code first, which uses a WCF
I have the following code that uses 'paste' and AWK script inside Perl. use
I have the following code that uses the SqlClient.ExecuteScalar method to return an ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.