如何用EF Fluent API映射Product实体的Vendors列表属性?
Got it, let's break down how to map your List<Guid> Vendors property in the Product entity to the Product_Vendor table using Entity Framework's Fluent API. Since Vendor_Id is just a standalone Guid from another database (no associated Vendor entity in your context), we're dealing with a value collection mapping to a join table.
Step 1: Define the Product Entity
First, make sure your Product class is set up with the Vendors property initialized to avoid null reference issues:
public class Product { public Guid Id { get; set; } public string Name { get; set; } public List<Guid> Vendors { get; set; } = new List<Guid>(); }
Step 2: Fluent API Configuration (EF 6)
If you're using Entity Framework 6, create an EntityTypeConfiguration class for Product to configure the mapping:
public class ProductConfiguration : EntityTypeConfiguration<Product> { public ProductConfiguration() { // Map Product to the Product table ToTable("Product"); HasKey(p => p.Id); // Map the Vendors list to the Product_Vendor join table HasMany(p => p.Vendors) .WithMany() // No reverse navigation (since there's no Vendor entity) .Map(m => { m.ToTable("Product_Vendor"); // Specify the join table name m.MapLeftKey("Product_Id"); // Foreign key column linking to Product.Id m.MapRightKey("Vendor_Id"); // Column storing the external Vendor Guid }); } }
Key Notes for EF 6:
WithMany()(no parameters) tells EF there's no corresponding navigation property on a related entity (since we don't have aVendorclass in our context).MapLeftKeyandMapRightKeyexplicitly map the columns in theProduct_Vendortable to theProduct's ID and the externalVendor_Idrespectively.
Step 3: Fluent API Configuration (EF Core)
EF Core doesn't use EntityTypeConfiguration by default (you can add it via third-party packages, but the native approach is to configure in OnModelCreating):
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Product>(entity => { entity.ToTable("Product"); entity.HasKey(p => p.Id); // Map the Vendors value collection to Product_Vendor entity.HasMany(p => p.Vendors) .WithMany() .UsingEntity<Dictionary<string, object>>( "Product_Vendor", // Join table name j => j.HasOne<Guid>().WithMany().HasForeignKey("Vendor_Id"), j => j.HasOne<Product>().WithMany().HasForeignKey("Product_Id"), j => { j.ToTable("Product_Vendor"); // Ensure column types match your SQL schema j.Property<Guid>("Vendor_Id").HasColumnType("uniqueidentifier"); j.Property<Guid>("Product_Id").HasColumnType("uniqueidentifier"); }); }); }
Key Notes for EF Core:
UsingEntity<Dictionary<string, object>>creates an anonymous intermediate entity to represent the join table, since we don't have a dedicated class for it.- We explicitly define the column types to match SQL Server's
uniqueidentifierfor both keys.
Important Checks
- Verify that your
Product_Vendortable has the exact column names specified (Product_IdandVendor_Id), both of typeuniqueidentifier. - Since
Vendor_Idcomes from another database, EF won't enforce any foreign key constraints here—this is just a read/write mapping for the Guid values.
内容的提问来源于stack exchange,提问作者Jelle Capenberghs

