ASP.NET Core 2.0 MVC EF一对多与多对多关系配置求助
Hey there! Let's work through this entity relationship issue together. I'll start by filling in the gaps of your classes and setting up the correct associations (I'm assuming you're using Entity Framework Core here, since it's the most common ORM for this kind of setup).
First, Fix the BaseEntity Class
Your original BaseEntity was missing a public modifier on the constructor, which would cause issues with instantiation:
public class BaseEntity { public long Id { get; set; } public DateTime Created { get; set; } public DateTime Modified { get; set; } public BaseEntity() { } // Added public modifier }
Complete the Entity Classes
Here's how each entity should look with proper navigation properties for the relationships:
Product Class
public class Product : BaseEntity { public string Name { get; set; } public decimal Price { get; set; } // Navigation for many-to-many with Category public ICollection<ProductCategory> ProductCategories { get; set; } = new List<ProductCategory>(); // Navigation for many-to-many with Tag public ICollection<ProductTag> ProductTags { get; set; } = new List<ProductTag>(); }
Category Class
public class Category : BaseEntity { public string Name { get; set; } public ICollection<ProductCategory> ProductCategories { get; set; } = new List<ProductCategory>(); }
Tag Class
public class Tag : BaseEntity { public string Name { get; set; } public ICollection<ProductTag> ProductTags { get; set; } = new List<ProductTag>(); }
Junction Tables (Many-to-Many Links)
These tables act as bridges between your main entities and need composite primary keys:
public class ProductCategory { public long ProductId { get; set; } public Product Product { get; set; } public long CategoryId { get; set; } public Category Category { get; set; } } public class ProductTag { public long ProductId { get; set; } public Product Product { get; set; } public long TagId { get; set; } public Tag Tag { get; set; } }
Configure Relationships in Your DbContext
EF Core won't automatically pick up the many-to-many relationships without explicit configuration. Add this to your OnModelCreating method:
protected override void OnModelCreating(ModelBuilder modelBuilder) { // Configure ProductCategory composite key and relationships modelBuilder.Entity<ProductCategory>() .HasKey(pc => new { pc.ProductId, pc.CategoryId }); modelBuilder.Entity<ProductCategory>() .HasOne(pc => pc.Product) .WithMany(p => p.ProductCategories) .HasForeignKey(pc => pc.ProductId); modelBuilder.Entity<ProductCategory>() .HasOne(pc => pc.Category) .WithMany(c => c.ProductCategories) .HasForeignKey(pc => pc.CategoryId); // Configure ProductTag composite key and relationships modelBuilder.Entity<ProductTag>() .HasKey(pt => new { pt.ProductId, pt.TagId }); modelBuilder.Entity<ProductTag>() .HasOne(pt => pt.Product) .WithMany(p => p.ProductTags) .HasForeignKey(pt => pt.ProductId); modelBuilder.Entity<ProductTag>() .HasOne(pt => pt.Tag) .WithMany(t => t.ProductTags) .HasForeignKey(pt => pt.TagId); // Optional: Set default values for BaseEntity's audit fields modelBuilder.Entity<BaseEntity>() .Property(b => b.Created) .HasDefaultValueSql("GETDATE()"); modelBuilder.Entity<BaseEntity>() .Property(b => b.Modified) .HasDefaultValueSql("GETDATE()") .ValueGeneratedOnAddOrUpdate(); }
Common Pitfalls to Avoid
- Always initialize navigation properties: Setting
= new List<>()prevents null reference exceptions when accessing related entities. - Composite keys are mandatory for junction tables: EF Core needs this to uniquely identify each link between entities.
- Public constructors on base entities: Without a public constructor, EF Core can't create instances of your entities during querying.
内容的提问来源于stack exchange,提问作者hguner

