There are three entities in my database (at least there should be three). 1. General Information about the offer. 2. Customer list 3. List of items in the offer. Initially, I proceeded from the fact that there would be only one customer in the offer and everything was fine: the database had three tables in accordance with the number of entities. But now I have a list of customers in the offer and a property to indicate who placed this one. This caused the appearance of a 4th table called "ProcurementDataCustomers". The table is empty, meaning that after parsing and filling in the data in other tables, it remains so. How can I make it so that when creating a database it does not automatically create this table? Is there any way to control this process?
public class ProcurementData
{
public ProcurementData()
{
this.PurchaseObjects = new List<PurchaseObject>();
this.Customers = new List<Customer>();
this.PublicationDate = new DateTime();
this.EndDate = new DateTime();
this.AuctionDate = new DateTime();
}
[Key]
public string Number { set; get; }
public string Name { set; get; }
public double InitialCost { set; get; }
public DateTime PublicationDate { set; get; }
public DateTime EndDate { set; get; }
public DateTime AuctionDate { set; get; }
public string ApplicanterName { set; get; }
public virtual ICollection<Customer> Customers { set; get; }
public virtual ICollection<PurchaseObject> PurchaseObjects { set; get; }
}
public class Customer
{
public Customer()
{
this.ProcurementData = new List<ProcurementData>();
this.PurchaseObjects = new List<PurchaseObject>();
}
[Key]
public string Name { set; get; }
public string INN { set; get; }
public string KPP { set; get; }
public virtual ICollection<ProcurementData> ProcurementData { set; get; }
public virtual ICollection<PurchaseObject> PurchaseObjects { set; get; }
}
public class PurchaseObject
{
public int Id { set; get; }
public string Name { set; get; }
public string OKPD2Code { set; get; }
public string MeasurementType { set; get; }
public double Amount { set; get; }
public double UnitPrice { set; get; }
public double TotalPrice { set; get; }
public string CustomerName { get; set; }
public virtual Customer Customer { get; set; }
public virtual ProcurementData ProcurementData { get; set; }
}
public class MyDbcontext : DbContext
{
public DbSet<ProcurementData> ProcurementData { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<PurchaseObject> PurchaseObjects { get; set; }
public MyDbcontext() : base(nameOrConnectionString: "Default")
{
Database.CreateIfNotExists();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<PurchaseObject>()
.HasRequired(p => p.ProcurementData)
.WithMany(p => p.PurchaseObjects);
modelBuilder.Entity<Customer>()
.HasMany(p => p.PurchaseObjects);
modelBuilder.Entity<ProcurementData>()
.HasMany(c => c.Customers)
.WithMany( p => p.ProcurementData);
base.OnModelCreating(modelBuilder);
}
}
Many to many relationships requires one extra table... Think about it.
Edit: If you want a bit of theory about relational DDBBs design begin here: https://en.wikipedia.org/wiki/First_normal_form
Continue with 2NF, 3... etc
Keeping it simple: if you have a ICollection<Customer>
in ProcurementData
and a ICollection<ProcurementData>
in Customer
you need a table storing the relationship Id-Id.