Create relationships with Entity Framework Core

asp.net-core-mvc c# database entity-framework entity-framework-core

Question

I want to build a database with Entity Framework Core. I use the command prompt and migrations to create the database. But as you can see on my diagram, I have a many-to-many relationship. How do I create this relationship with my classes below?

enter image description here

Code:

public class ShoppingDbContext : IdentityDbContext<User>
{
    public ShoppingDbContext(DbContextOptions options) : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder     optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
    }

    public DbSet<Order> Orders { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<PartCategory> PartCategory { get; set; }
    public DbSet<Part> Parts { get; set; }
}

public class Product 
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }

    public List<PartCategory> PartCategory { get; set; } 
}

public class PartCategory
{
    public int PartCategoryId { get; set; }
    public string Category { get; set; }

    public List<Part> Parts { get; set; }
}

//update

public class ProductPartCategory
{
public int ProductId { get; set; }
public Product Product { get; set; }

public int PartCategoryId { get; set; }
public PartCategory PartCategory { get; set; }
}

public class Product 
{

public int ProductId { get; set; }
public string ProductName { get; set; }
public double Price { get; set; }
public List<PartCategory> PartCategories{ get; set; } 

}

 public class PartCategory
{
public int PartCategoryId { get; set; }
public string Category { get; set; }
public List<Product> Products { get; set; }

//dont mind this propertie its for other stuff
public List<Part> Parts { get; set; }
}
1
1
9/23/2016 7:42:18 AM

Accepted Answer

You can try as shown below using Fluent API.

Note :

Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.

public class ShoppingDbContext: DbContext
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<PartCategory> PartCategories{ get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ProductPartCategory>()
                .HasKey(t => new { t.ProductId, t.PartCategoryId });

            modelBuilder.Entity<ProductPartCategory>()
                .HasOne(pt => pt.Product)
                .WithMany(p => p.ProductPartCategories)
                .HasForeignKey(pt => pt.ProductId);

            modelBuilder.Entity<ProductPartCategory>()
                .HasOne(pt => pt.PartCategory)
                .WithMany(t => t.ProductPartCategories)
                .HasForeignKey(pt => pt.PartCategoryId);
        }
    }

Your models should be like this :

 public class Product 
  {

    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public List<ProductPartCategory> ProductPartCategories { get; set; } 

  }

 public class PartCategory
   {
     public int PartCategoryId { get; set; }
     public string Category { get; set; }
     public List<ProductPartCategory> ProductPartCategories { get; set; } 
   }

  public class ProductPartCategory
    {
      public int ProductId { get; set; }
      public Product Product { get; set; }

      public int PartCategoryId { get; set; }
      public PartCategory PartCategory { get; set; }
   }
0
9/24/2016 5:15:48 AM

Popular Answer

You've already gotten some answers, but here is how you can find out for yourself...

The way I answer all of my EF modeling questions is to have dotnet reverse engineer the data structure for me. By executing a scaffolding command in a command line prompt (from inside of your project files directory), dotnet will create a bunch of files that you can then either use directly or examine for structure and delete to your liking.

The only way this will work is if you have all of your relationships (foreign keys, etc) set up properly in your development DB.

dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -a -t PartCategory -t Products -t ProducsPartCategory --o OutputDirectory


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow