0

I am having a slight problem when building my Many to Many relation with Entity Framework. I try to make a many to many relationship between ConnectionPoint and and Route, via the ConnectionPointRoute. But on executing the DB update (Run the seed), I get the message "Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.". I am probably just overlooking something but any help is appreciated!

public class ConnectionPoint
{
    public int ConnectionPointId { get; set; }
    public string ConnectionPointName { get; set; }
    public virtual Location Location { get; set; }
    public virtual ICollection<ConnectionPointRoute> ConnectionPointRoutes { get; set; }
    public virtual ICollection<Connection> Connections { get; set; }
}

public class Route
{
    public int RouteId { get; set; }
    public string RouteName { get; set; }
    public virtual ICollection<ConnectionPointRoute> ConnectionPointRoutes { get; set; }
}

public class ConnectionPointRoute
{
    public int ConnectionPointId { get; set; }
    public int RouteId { get; set; }
    public  int Position { get; set; }
    public virtual ICollection<ConnectionPoint> ConnectionPoints { get; set; }
    public virtual ICollection<Route> Routes { get; set; }
}
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<ConnectionPointRoute>()
            .HasKey(c => new { c.ConnectionPointId, c.RouteId });

        modelBuilder.Entity<ConnectionPoint>()
            .HasMany(c => c.ConnectionPointRoutes)
            .WithRequired()
            .HasForeignKey(c => c.ConnectionPointId);

        modelBuilder.Entity<Route>()
            .HasMany(c => c.ConnectionPointRoutes)
            .WithRequired()
            .HasForeignKey(c => c.RouteId);
    }
        var ConnectionPointRoutes = new List<ConnectionPointRoute>
        {
            new ConnectionPointRoute { Routes = new List<Route>(), ConnectionPoints= new List<ConnectionPoint>(), Position = 1},
            new ConnectionPointRoute { Routes = new List<Route>(), ConnectionPoints= new List<ConnectionPoint>(), Position = 2},
            new ConnectionPointRoute { Routes = new List<Route>(), ConnectionPoints= new List<ConnectionPoint>(), Position = 2},
            new ConnectionPointRoute { Routes = new List<Route>(), ConnectionPoints= new List<ConnectionPoint>(), Position = 1}
        };
        ConnectionPointRoutes.ForEach(r => context.ConnectionPointRoutes.AddOrUpdate(r));
        context.SaveChanges();
Litsher - Nathan
  • 265
  • 1
  • 5
  • 18
  • I doubt EF supports composite key with autoincrement - the default would be DatabaseGeneratedOption.None, so it sends those entries as it is to the database (all with both key columns set to default(int)=0), and there you'd expect this error to occur. set the keys to correct distinct values or edit the key to fix this issue. – DevilSuichiro Mar 22 '16 at 12:26
  • Sorry, I don't think I follow you, could you be a little more specific? Thanks in advance! – Litsher - Nathan Mar 22 '16 at 12:45
  • I think you are running into the same issue as this: http://stackoverflow.com/questions/14751898/how-to-solve-combined-one-to-one-and-one-to-many-relationship-in-ef-5-code-first – Steve Greene Mar 22 '16 at 14:37
  • I didn't get an answer there, I feel my problem is more complicated. My guess is that Entity needs an Identifier, but I have no use for it as it is only a relational field – Litsher - Nathan Mar 22 '16 at 15:12

1 Answers1

1

Your ConnectionPointRoute has two List<>. Do you want a many-to-many between ConnectionPoint and ConnectionPointRoute and another many-to-many between ConnectionPointRoute and Route (Option #1) ?

Or are you trying to do a many-to-many between ConnectionPoint and Route where ConnectionPointRoute is the join table (Option #2) ?

I'm guessing that you wanted Option #2. It would be configured like this:

public class ConnectionPointRoute
{
    public int ConnectionPointId { get; set; }

    public int RouteId { get; set; }

    public int Position { get; set; }

    public virtual ConnectionPoint ConnectionPoint { get; set; }

    public virtual Route Route { get; set; }
}

Model Builder:

modelBuilder.Entity<ConnectionPointRoute>()
    .HasKey(c => new { c.ConnectionPointId, c.RouteId });

modelBuilder.Entity<ConnectionPoint>()
    .HasMany(c => c.ConnectionPointRoutes)
    .WithRequired(x => x.ConnectionPoint)
    .HasForeignKey(c => c.ConnectionPointId);

modelBuilder.Entity<Route>()
    .HasMany(c => c.ConnectionPointRoutes)
    .WithRequired(x => x.Route)
    .HasForeignKey(c => c.RouteId);

And to create:

new ConnectionPointRoute { Route = new Route(), ConnectionPoint= new ConnectionPoint(), Position = 1}
Chris
  • 2,009
  • 1
  • 16
  • 25