Hakkinen Hakkinen - 23 days ago 10
SQL Question

EF Wont generate connection table

I´m trying to connect two tables but i don´t understand why it not creates the connection table. I would be really thankful if any one could take a peek.

Thanks in advance! :)

Edit: Included the datacontext. The table User and Project are created successfully but don´t get the dbo.ProjectUser...

User model:

public class User : IdentityUser
{

public string FirstName { get; set; }

public string SurName { get; set; }

public string Address { get; set; }

public string Domain { get; set; }

public DateTime? TimeInDomain { get; set; }

public DateTime? TimeInCompany { get; set; }

public Project Project { get; set; }

public ICollection<Project> Projects { get; set; }

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{

// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}


Project model:

public class Project
{
public int Id { get; set; }

public string Platform { get; set; }

public DateTime ProjectStart { get; set; }

public DateTime ProjectLaunch { get; set; }

public string Description { get; set; }

public bool IsActive { get; set; }

public ICollection<User> Users { get; set; }
}


Datacontext:

public class DataContext : IdentityDbContext<User>
{
public DbSet<Project> Projects {get; set; }

public DataContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}

public static DataContext Create()
{
return new DataContext();
}

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users");
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
modelBuilder.Entity<IdentityRole>().ToTable("Roles");
}
}

Answer Source

User has two properties referring to Project:

public Project Project { get; set; }
public ICollection<Project> Projects { get; set; }

By default (without a User property in Project), EF will consider the first property as one end of a one-to-many relationship. Normally, it will also assume that the other end is Project.Users. Apparently (I didn't know that), the presence of User.Projects makes EF decide that all associations are independent of one another: User has two FKs to Project, Project one FK to User. I think EF should have thrown a model builder exception telling that the other end of Project.Users is ambiguous.

So you have to tell EF how you intend the associations to coexist:

modelBuilder.Entity<User>()
    .HasMany(u => u.Projects)
    .WithMany(p => p.Users)
    .Map(m => m.MapLeftKey("UserId").MapRightKey("ProjectId"));
modelBuilder.Entity<User>()
    .HasOptional(u => u.Project) // Or HasRequired
    .WithMany().Map(m => m.MapKey("ProjectId")).WillCascadeOnDelete(false);