itnifl
asked on
Entity Framework many-to-many relationship not getting set up in code first approach
So in my User class I have this:
In my DataSystem cass I gave this:
This magic usually does the trick to create a many to many relationship. Here I want that magic to happen between User and DataSystem. But it doesn't happen. When looking at the generated database, there is no in between table DataSystemUser or anything similar.
So, optimistic as I am, I Google a bit and try this:
But then my  browser fills with nasty messages saying such as:
I don't give up just yet. I try this:'
But Entity Framework still is not letting me off the hook:
So how do I do this right?
public virtual ICollection<DataSystem> Systems { get; set; }
In my DataSystem cass I gave this:
public virtual ICollection<User> Users { get; set; }
This magic usually does the trick to create a many to many relationship. Here I want that magic to happen between User and DataSystem. But it doesn't happen. When looking at the generated database, there is no in between table DataSystemUser or anything similar.
So, optimistic as I am, I Google a bit and try this:
modelBuilder.Entity<User>()
.HasMany(v => v.Systems)
.WithMany(p => p.Users);
But then my  browser fills with nasty messages saying such as:
Exception Details: System.Data.SqlClient.SqlException: Introducing FOREIGN KEY constraint 'FK_dbo.UserDataSystems_db o.DataSyst ems_DataSy stem_DataS ystemId' on table 'UserDataSystems' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index.
I don't give up just yet. I try this:'
modelBuilder.Entity<DataSystem>().
HasOptional(e => e.Users).
WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>().
HasOptional(e => e.Systems).
WithMany()
.WillCascadeOnDelete(false);
But Entity Framework still is not letting me off the hook:
Exception Details: System.Data.SqlClient.SqlException: Introducing FOREIGN KEY constraint 'FK_dbo.UserDataSystems_db o.DataSyst ems_DataSy stem_DataS ystemId' on table 'UserDataSystems' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index.
So how do I do this right?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER