Voglio creare un database con Entity Framework Core. Io uso il prompt dei comandi e le migrazioni per creare il database. Ma come puoi vedere sul mio diagramma, ho una relazione molti-a-molti. Come creo questa relazione con le mie classi qui sotto?
Codice:
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; }
}
//aggiornare
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; }
}
Puoi provare come mostrato di seguito utilizzando Fluent API .
Nota :
Le relazioni many-to-many senza una classe entity per rappresentare la tabella join non sono ancora supportate. Tuttavia, è possibile rappresentare una relazione molti-a-molti includendo una classe di entità per la tabella di join e mappando due relazioni uno-a-molti separate.
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);
}
}
I tuoi modelli dovrebbero essere così:
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; }
}
Hai già ottenuto alcune risposte, ma ecco come puoi scoprirlo da te ...
Il modo in cui rispondo a tutte le mie domande di modellazione EF è di avere dotnet reverse engineer per me. Eseguendo un comando di scaffolding in un prompt della riga di comando (all'interno della directory dei file di progetto), dotnet creerà un gruppo di file che potrai utilizzare direttamente oppure esaminare per la struttura ed eliminare a tuo piacimento.
L'unico modo in cui funziona è se tutte le tue relazioni (chiavi esterne, ecc.) Sono configurate correttamente nel tuo DB di sviluppo.
dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -a -t PartCategory -t Products -t ProducsPartCategory --o OutputDirectory