Why is Entity Framework Core attempting to insert records into one of the tables from many to many relationships and NOT the join table?

0

Given the following set up where there are many Teams and there are many LeagueSessions. Each Team belongs to zero or more LeagueSessions but only ever one LeagueSession is active. LeagueSessions have many teams, and the teams will be repeated. Many-to-many relationship is established between Teams and LeagueSessions with a join table called TeamsSessions.

Team model looks like this:

public class Team
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public League League { get; set; }
        public string LeagueID { get; set; }        
        public bool Selected { get; set; }
        public ICollection<Match> Matches { get; set; }
        public virtual ICollection<TeamSession> TeamsSessions { get; set; }
    }

Team model fluent api configuration:

`
public class TeamConfiguration
    {        
        public TeamConfiguration(EntityTypeBuilder<Team> model)
        {
            // The data for this model will be generated inside ThePLeagueDataCore.DataBaseInitializer.DatabaseBaseInitializer.cs class
            // When generating data for models in here, you have to provide it with an ID, and it became mildly problematic to consistently get
            // a unique ID for all the teams. In ThePLeagueDataCore.DataBaseInitializer.DatabaseBaseInitializer.cs we can use dbContext to generate
            // unique ids for us for each team.

            model.HasOne(team => team.League)
                .WithMany(league => league.Teams)
                .HasForeignKey(team => team.LeagueID);   
        }

    }
`

Each team belongs to a single League. League model looks like this:

`public class League
    {
        public string Id { get; set; }
        public string Type { get; set; }
        public string Name { get; set; }
        public IEnumerable<Team> Teams { get; set; }
        public bool Selected { get; set; }        
        public string SportTypeID { get; set; }
        public SportType SportType { get; set; }
        public IEnumerable<LeagueSessionSchedule> Sessions { get; set; }

    }`

fluent API for the League:

`public LeagueConfiguration(EntityTypeBuilder<League> model)
        {
            model.HasOne(league => league.SportType)
                .WithMany(sportType => sportType.Leagues)
                .HasForeignKey(league => league.SportTypeID);

            model.HasMany(league => league.Teams)
                .WithOne(team => team.League)
                .HasForeignKey(team => team.LeagueID);

            model.HasData(leagues);
        }`

SessionScheduleBase class looks like this:

public class SessionScheduleBase
    {
        public string LeagueID { get; set; }
        public bool ByeWeeks { get; set; }
        public long? NumberOfWeeks { get; set; }
        public DateTime SessionStart { get; set; }
        public DateTime SessionEnd { get; set; }
        public ICollection<TeamSession> TeamsSessions { get; set; } = new Collection<TeamSession>();
        public ICollection<GameDay> GamesDays { get; set; } = new Collection<GameDay>();
    }

Note: LeagueSessionSchedule inherits from SessionScheduleBase

The TeamSession model looks like this:

`public class TeamSession
    {
        public string Id { get; set; }
        public string TeamId { get; set; }
        public Team Team { get; set; }
        public string LeagueSessionScheduleId { get; set; }
        public LeagueSessionSchedule LeagueSessionSchedule { get; set; }
    }`

I then configure the relationship with the fluent API like this:

`public TeamSessionConfiguration(EntityTypeBuilder<TeamSession> model)
        {

            model.HasKey(ts => new { ts.TeamId, ts.LeagueSessionScheduleId });            
            model.HasOne(ts => ts.Team)
                .WithMany(t => t.TeamsSessions)
                .HasForeignKey(ts => ts.TeamId);
            model.HasOne(ts => ts.LeagueSessionSchedule)
                .WithMany(s => s.TeamsSessions)
                .HasForeignKey(ts => ts.LeagueSessionScheduleId);
        }`

The problem arises whenever I attempt to insert a new LeagueSessionSchedule. The way I am adding a new TeamSession object onto the new LeagueSessionSchedule is like this:

`foreach (TeamSessionViewModel teamSession in newSchedule.TeamsSessions)
    {                 
        Team team = await this._teamRepository.GetByIdAsync(teamSession.TeamId, ct);

            if(team != null)
            {
                TeamSession newTeamSession = new TeamSession()
                {
                    Team = team,                            
                    LeagueSessionSchedule = leagueSessionSchedule
                };

                leagueSessionSchedule.TeamsSessions.Add(newTeamSession);
            }
    }`

Saving the new LeagueSessionSchedule code:

public async Task<LeagueSessionSchedule> AddScheduleAsync(LeagueSessionSchedule newLeagueSessionSchedule, CancellationToken ct = default)
{
    this._dbContext.LeagueSessions.Add(newLeagueSessionSchedule);
    await this._dbContext.SaveChangesAsync(ct);

    return newLeagueSessionSchedule;
}

Saving the new LeagueSessionSchedule object throws an error by Entity Framework Core that it cannot INSERT a duplicate primary key value into the dbo.Teams table. I have no idea why its attempting to add to dbo.Teams table and not into TeamsSessions table.

ERROR:

INSERT INTO [LeagueSessions] ([Id], [Active], [ByeWeeks], [LeagueID], [NumberOfWeeks], [SessionEnd], [SessionStart])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);
INSERT INTO [Teams] ([Id], [Discriminator], [LeagueID], [Name], [Selected])
VALUES (@p7, @p8, @p9, @p10, @p11),
(@p12, @p13, @p14, @p15, @p16),
(@p17, @p18, @p19, @p20, @p21),
(@p22, @p23, @p24, @p25, @p26),
(@p27, @p28, @p29, @p30, @p31),
(@p32, @p33, @p34, @p35, @p36),
(@p37, @p38, @p39, @p40, @p41),
(@p42, @p43, @p44, @p45, @p46);

System.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_Teams'. Cannot insert duplicate key in object 'dbo.Teams'. The duplicate key value is (217e2e11-0603-4239-aab5-9e2f1d3ebc2c).

My goal is to create a new LeagueSessionSchedule object. Along with the creation of this object, I also have to create a new TeamSession entry to the join table (or not if join table is not necessary) to then be able to pick any given team and see what session it is currently a part of.

My entire PublishSchedule method is the following:

`
public async Task<bool> PublishSessionsSchedulesAsync(List<LeagueSessionScheduleViewModel> newLeagueSessionsSchedules, CancellationToken ct = default(CancellationToken))
        {
            List<LeagueSessionSchedule> leagueSessionOperations = new List<LeagueSessionSchedule>();

            foreach (LeagueSessionScheduleViewModel newSchedule in newLeagueSessionsSchedules)
            {
                LeagueSessionSchedule leagueSessionSchedule = new LeagueSessionSchedule()
                {
                    Active = newSchedule.Active,
                    LeagueID = newSchedule.LeagueID,
                    ByeWeeks = newSchedule.ByeWeeks,
                    NumberOfWeeks = newSchedule.NumberOfWeeks,
                    SessionStart = newSchedule.SessionStart,
                    SessionEnd = newSchedule.SessionEnd                    
                };

                // leagueSessionSchedule = await this._sessionScheduleRepository.AddScheduleAsync(leagueSessionSchedule, ct);

                // create game day entry for all configured game days
                foreach (GameDayViewModel gameDay in newSchedule.GamesDays)
                {
                    GameDay newGameDay = new GameDay()
                    {
                        GamesDay = gameDay.GamesDay
                    };

                     // leagueSessionSchedule.GamesDays.Add(newGameDay);

                    // create game time entry for every game day
                    foreach (GameTimeViewModel gameTime in gameDay.GamesTimes)
                    {
                        GameTime newGameTime = new GameTime()
                        {
                            GamesTime = DateTimeOffset.FromUnixTimeSeconds(gameTime.GamesTime).DateTime.ToLocalTime(),
                            // GameDayId = newGameDay.Id
                        };

                        // newGameTime = await this._sessionScheduleRepository.AddGameTimeAsync(newGameTime, ct);                        
                        newGameDay.GamesTimes.Add(newGameTime);
                    }

                    leagueSessionSchedule.GamesDays.Add(newGameDay);
                }

                // update teams sessions
                foreach (TeamSessionViewModel teamSession in newSchedule.TeamsSessions)
                {
                    // retrieve the team with the corresponding id
                    Team team = await this._teamRepository.GetByIdAsync(teamSession.TeamId, ct);

                    if(team != null)
                    {
                        TeamSession newTeamSession = new TeamSession()
                        {
                            Team = team,                            
                            LeagueSessionSchedule = leagueSessionSchedule
                        };

                        leagueSessionSchedule.TeamsSessions.Add(newTeamSession);
                    }
                }

                // update matches for this session
                foreach (MatchViewModel match in newSchedule.Matches)
                {
                    Match newMatch = new Match()
                    {
                        DateTime = match.DateTime,
                        HomeTeamId = match.HomeTeam.Id,
                        AwayTeamId = match.AwayTeam.Id,
                        LeagueID = match.LeagueID                        
                    };

                    leagueSessionSchedule.Matches.Add(newMatch);
                }

                try
                {
                    leagueSessionOperations.Add(await this._sessionScheduleRepository.AddScheduleAsync(leagueSessionSchedule, ct));
                }
                catch(Exception ex)
                {

                }
            }

            // ensure all leagueSessionOperations did not return any null values
            return leagueSessionOperations.All(op => op != null);
        }
`
c#
entity-framework-core
asked on Stack Overflow Dec 31, 2019 by O.MeeKoh • edited Dec 31, 2019 by O.MeeKoh

1 Answer

2

This is not a many-to-many relationship.

It is two separate one-to-many relationships, which happen to refer to the same table on one end of the relationship.

While it is true that on the database level, both use cases are represented by three tables, i.e. Foo 1->* FooBar *<-1 Bar, these two cases are treated differently by Entity Framework's automated behavior - and this is very important.

EF only handles the cross table for you if it is a direct many-to-many, e.g.

public class Foo
{
    public virtual ICollection<Bar> Bars { get; set; }
}

public class Bar
{
    public virtual ICollection<Foo> Foos { get; set; }
}

EF handles the cross table behind the scenes, and you are never made aware of the existence of the cross table (from the code perspective).

Importantly, EF Core does not yet support implicit cross tables! There is currently no way to do this in EF Core, but even if there were, you're not using it anyway, so the answer to your problem remains the same regardless of whether you're using EF or EF Core.

However, you have defined your own cross table. While this is still representative of a many-to-many relationship in database terms, it has ceased to be a many-to-many relationship as far as EF is concerned, and any documentation you find on EF's many-to-many relationships no longer applies to your scenario.


Unattached but indirectly added objects are assumed to be new.

By "indirectly added", I mean you that it was added to the context as part of another entity (which you directly added to the context). In the following example, foo is directly added and bar is indirectly added:

var foo = new Foo();
var bar = new Bar();

foo.Bar = bar;

context.Foos.Add(foo);   // directly adding foo
                         // ... but not bar
context.SaveChanges();

When you add (and commit) a new entity to the context, EF adds it for you. However, EF also looks at any related entities that the first entity contains. During the commit in the above example, EF will look at both the foo and bar entities and will handle them accordingly. EF is smart enough to realize that you want bar to be stored in the database since you put it inside the foo object and you explicitly asked EF to add foo to the database.

It is important to realize that you've told EF that foo should be created (since you called Add(), which implies a new item), but you never told EF what it should do with bar. It's unclear (to EF) what you expect EF to do with this, and thus EF is left guessing at what to do.

If you never explained to EF whether bar already exists or not, Entity Framework defaults to assuming it needs to create this entity in the database.

Saving the new LeagueSessionSchedule object throws an error by Entity Framework Core that it cannot INSERT a duplicate primary key value into the dbo.Teams table. I have no idea why its attempting to add to dbo.Teams table

Knowing what you now know, the error becomes clearer. EF is trying to add this team (which was the bar object in my example) because it has no information on this team object and what its state in the database is.

There are a few solutions here.

1. Use the FK property instead of the navigational property

This is my preferred solution because it leaves no room for error. If the team ID does not yet exist, you get an error. At no point will EF try to create a team, since it doesn't even know the team's data, it only knows the (alleged) ID you're trying to create a relationship with.

Note: I am omitting LeagueSessionSchedule as it is unrelated to the current error - but it's essentially the same behavior for both Team and LeagueSessionSchedule.

TeamSession newTeamSession = new TeamSession()
{
    TeamId = team.Id                           
};

By using the FK property instead of the nav prop, you are informing EF that this is an existing team - and therefore EF no longer tries to (re)create this team.

2. Ensure that the team is tracked by the current context

Note: I am omitting LeagueSessionSchedule as it is unrelated to the current error - but it's essentially the same behavior for both Team and LeagueSessionSchedule.

context.Teams.Attach(team);

TeamSession newTeamSession = new TeamSession()
{
    Team = team
};

By attaching the object to the context, you are informing it of its existence. The default state of a newly attached entity is Unchanged, meaning "this already exists in the database and has not been changed - so you don't need to update it when we commit the context".

If you have actually made changes to your team that you want to be updated during commit, you should instead use:

context.Entry(team).State = EntityState.Modified;

Entry() inherently also attaches the entity, and by setting its state to Modified you ensure that the new values will be committed to the database when you call SaveChanges().


Note that I prefer solution 1 over solution 2 because it's foolproof and much less likely to lead to unexpected behavior or runtime exceptions.


String primary keys are undesirable

I'm not going to say that it doesn't work, but strings cannot be autogenerated by Entity Framework, making them undesirable as the type of your entity's PK. You will need to manually set your entity PK values.

Like I said, it's not impossible, but your code shows that you're not explicitly setting PK values:

if(team != null)
{
    TeamSession newTeamSession = new TeamSession()
    {
        Team = team,                            
        LeagueSessionSchedule = leagueSessionSchedule
    };

    leagueSessionSchedule.TeamsSessions.Add(newTeamSession);
}

If you want your PK's to be automatically generated, use an appropriate type. int and Guid are by far the most commonly used types for this.

Otherwise, you're going to have to start setting your own PK values, because if you don't (and the Id value thus defaults to null), your code is going to fail when you add a second TeamSession object using the above code (even though you're doing everything else correctly), since PK null is already taken by the first entity you added to the table.

answered on Stack Overflow Dec 31, 2019 by Flater • edited Dec 31, 2019 by Flater

User contributions licensed under CC BY-SA 3.0