Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Many to Many Database Migration

I am building ASP MVC web site using Entity Framework 4.4 with .NET Framework 4.0

I've add to my model a many to many relation like so:

  public class User {
    public int UserID { get; set; }
    public string Username { get; set; }
    public virtual ICollection<Tenant> Tenants { get; set; }
  }


  public class Tenant {
    public string TenantID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<User> Users { get; set; }
  }

When I run Add-Migration command I get this migration class (I remove the Down method)

  public partial class TenantUsersManyToManyMigration : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.UserTenants",
                c => new
                    {
                        User_UserID = c.Int(nullable: false),
                        Tenant_TenantID = c.String(nullable: false, maxLength: 128),
                    })
                .PrimaryKey(t => new { t.User_UserID, t.Tenant_TenantID })
                .ForeignKey("dbo.Users", t => t.User_UserID, cascadeDelete: true)
                .ForeignKey("dbo.Tenants", t => t.Tenant_TenantID, cascadeDelete: true)
                .Index(t => t.User_UserID)
                .Index(t => t.Tenant_TenantID);
        }
  }
  1. Why are the field names for TenantID and UserID are User_UserID and Tenant_TenantID and not UserID and TenantID, respectively.

  2. How can I change the default migration scaffolding (or my model) to make cascadeDelete to be false? (currently I simply change it by hand).

like image 810
Ido Ran Avatar asked Sep 12 '12 09:09

Ido Ran


People also ask

How does Entity Framework handle many-to-many relationships?

The many-to-may relationship can be achieved using HasMany and WithMany methods. The default conventions for many-to-many relationships creates a joining table with the default naming conventions. You can customize a joining table name and column names using Fluent API.

What is meant by many-to-many relationship in EF core?

Many-to-many relationships require a collection navigation property on both sides. They will be discovered by convention like other types of relationships. The way this relationship is implemented in the database is by a join table that contains foreign keys to both Post and Tag .

Can we use multiple database in Entity Framework?

Multiple DbContext was first introduced in Entity Framework 6.0. Multiple context classes may belong to a single database or two different databases.

How do you add migration to multiple contexts?

Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.


1 Answers

You can create your mapping table the way you're wanting using fluent notation. In your DbContext class, override the OnModelCreating with this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .HasMany(u => u.Tenants)
            .WithMany(t => t.Users)
            .Map(m =>
                {
                    m.ToTable("UserTenants");
                    m.MapLeftKey("UserId");
                    m.MapRightKey("TenantId");
                });
    }

Also, using fluent, if you want to disable cascade deleting on individual tables, you can use the .WillCascadeDelete(false) when mapping properties. Here's a great post on MSDN on how to use fluent notations.

like image 132
Mark Oreta Avatar answered Nov 07 '22 23:11

Mark Oreta