Im trying to do a one-to-many map by using fluent api.
This is my classes
public class Product : EntityBase
{
public Product()
{
this.ProductArticles = new List<ProductArticle>();
}
[Key]
public int ProductId { get; set; }
public string Description { get; set; }
public string ReportText1 { get; set; }
public string ReportText2 { get; set; }
public bool Standard { get; set; }
public int ProductGroupId { get; set; }
public decimal? Surcharge1 { get; set; }
public decimal? Surcharge2 { get; set; }
public decimal? Surcharge3 { get; set; }
public decimal? Surcharge4 { get; set; }
public decimal PriceIn { get; set; }
public decimal PriceOut { get; set; }
public decimal PriceArtisanIn { get; set; }
public decimal PriceArtisanOut { get; set; }
public decimal PriceTotalIn { get; set; }
public decimal PriceTotalOut { get; set; }
public decimal PriceTotalOutVat { get; set; }
public decimal PriceAdjustment { get; set; }
public bool Calculate { get; set; }
public string Notes { get; set; }
[ForeignKey("ProductGroupId")]
public virtual ProductGroup ProductGroup { get; set; }
public virtual ICollection<ProductArticle> ProductArticles { get; set; }
}
public class ProductArticle : EntityBase
{
[Key]
public int ProductArticleId { get; set; }
public int ProductId { get; set; }
public int ArticleId { get; set; }
public decimal Qty { get; set; }
public decimal PriceIn { get; set; }
public bool Primary { get; set; }
public virtual Product Product { get; set; }
public virtual Article Article { get; set; }
}
Now i want from single Product include all ProductArticles
This is my mapping
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
// Primary Key
this.HasKey(p => p.ProductId);
// Table & Column Mappings
this.ToTable("Product");
this.HasMany(p => p.ProductArticles)
.WithOptional()
.Map(p => p.MapKey("ProductId").ToTable("ProductArticle"));
}
But it doesnt work.. Please help :)
First - by convention EF treats property with name equal to Id or EntityTypeName + Id is a primary key. So, you don't need to configure that manually.
Second - if you don't want table names to be plural, just remove that convention from your context instead of providing table name for each entity mapping:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
And last - EF smart enough to define foreign keys which have names like RelatedEntityTypeName + Id. So, you don't need any fluent configurations here.
Related
I have an "Item" table defined as such:
item_id
Name
Description
itemseries_id
itemtype_id
itemcondition_id
And then I have "ItemForSale" table:
itemforsale_id
item_id
price
date_added
Entities:
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int ItemSeriesId { get; set; }
public ItemSeries ItemSeries { get; set; }
public int ItemConditionId { get; set; }
public ItemCondition ItemCondition { get; set; }
public int ItemTypeId { get; set; }
public ItemType ItemType { get; set; }
public List<ItemTag> ItemTags { get; set; }
public List<ItemImage> ItemImages { get; set; }
public List<ItemPurchase> ItemPurchases { get; set; }
public List<ItemSale> ItemSales { get; set; }
}
public class ItemForSale
{
public int ItemForSaleId { get; set; }
public decimal Price { get; set; }
public DateTime AddedDate { get; set; }
public int ItemId { get; set; }
public Item Item { get; set; }
}
How would I use the FluentAPI between these? I know I could add a reference to ItemForSale inside the Item entity class, but it doesn't make sense to me. So far I have mapped all of my One-to-one and many-to-many relationships, but the relationship between Item and ItemForSale is just confusing me.
Note: I am distinguishing between items that have been sold as a "Sale" or "ItemSale" and an item up for sale with no buyer as "ItemForSale"
From the EF Core Docs, you can do something like this:
class MyContext : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<ItemSale> ItemSales { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ItemSale>()
.HasOne(p => p.Item)
.WithMany(b => b.ItemSales)
.HasForeignKey(p => p.ItemId)
.IsRequired(false);
}
}
public class Item
{
public int ItemId { get; set; }
public string Url { get; set; }
public List<ItemSale> ItemSales { get; set; }
}
public class ItemSale
{
public int ItemSaleId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public Item Item { get; set; }
// note that the reference id is nullable
public int? ItemId { get; set; }
}
And then, mark the property ItemId in your model class as int?. While on EF 6, we had the HasOptional configuration option, but on EF Core, if the reference property can be nullable, it assumes the property starts from 0, like 0..N. I think that even the IsRequired(false) is not needed in this context as well, but here it goes.
I want to configure a one-to-many relationship in Ef core. As you see I have a class for order and the other one for OrderItems.
I do it when I use NHibernate.of course, I consider orderItem class as ValueObject.But I want to do it using EF Core.
public class Order
{
public long Id { get; set; }
public long CustomerId { get; set; }
public DateTime OrderDateTime { get; set; }
public ICollection<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
public string BookId { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal? Discount { get; set; }
public decimal Total { get; set; }
public Order Order { get; set; }
}
you should define meta data [ForeignKey] with type of primary key in order entity. after that ef core automatically set in db by your chosen name
public class Order
{
public long Id { get; set; }
public long CustomerId { get; set; }
public DateTime OrderDateTime { get; set; }
public virtual ICollection<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
public long Id { get; set; }
[ForeignKey(nameof(OrderId)]
public virtual Order Order { get; set; }
public long OrderId { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal? Discount { get; set; }
public decimal Total { get; set; }
}
Here is a simple demo like below:
1.Model:
public class Order
{
public long Id { get; set; }
public long CustomerId { get; set; }
public DateTime OrderDateTime { get; set; }
public ICollection<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
public int OrderItemId { get; set; }//you need to define a primary key for OrderItem model
public string BookId { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal? Discount { get; set; }
public decimal Total { get; set; }
public Order Order { get; set; }
}
2.DbContext:
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public DbSet<Order> Orders { get; set; }
public DbSet<OrderItem> OrderItems { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.HasMany(c => c.OrderItems)
.WithOne(e => e.Order);
}
}
3.Startup.cs:
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
4.appsettings.json:
"ConnectionStrings": {
"MyDbContext": "Server=(localdb)\\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
}
5.Run command line on Package Nuget Manager:
PM>add-migration init
PM>update-database
Model
public class Order {
public long Id { get; set; }
public long CustomerId { get; set; }
public DateTime OrderDateTime { get; set; }
public virtual ICollection<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
[Key]
public int OrderItemId { get; set; }
public string BookId { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal? Discount { get; set; }
public decimal Total { get; set; }
public long OrderId{get;set;} // ForeignKey OrderId
[ForeignKey("OrderId")]
public virtual Order Order { get; set; }
}
// open PackageManager console
PM>add-migration "orderItem changed"
PM>update-database
In my regular .NET Framework application, I was using EF 6.x and was also using some Inheritance, specifically:
PurchaseOrder.cs and SaleOrder.cs both inherit from Order.cs
And in the OnModelCreating() on my context class inheriting from IdentityDbContext, I was doing:
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
This used to work, but now I am moving my application to .NET Core 2.0 and I am using EF Core. What achieves the same thing in EF Core? Because right now I am getting the error:
System.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Order_Business_CustomerId' on table 'Order' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
UPDATE
Here's the code after Ahmar's answer. In my context class, I have:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.HasDefaultSchema("PD");
builder.Entity<Customer>()
.HasMany(c => c.SaleOrders)
.WithOne(e => e.Customer)
.OnDelete(DeleteBehavior.SetNull);
builder.Entity<Supplier>()
.HasMany(po => po.PurchaseOrders)
.WithOne(e => e.Supplier)
.OnDelete(DeleteBehavior.SetNull);
builder.Entity<PurchaseOrder>()
.HasMany(li => li.LineItems)
.WithOne(po => po.PurchaseOrder)
.OnDelete(DeleteBehavior.SetNull);
builder.Entity<SaleOrder>()
.HasMany(li => li.LineItems)
.WithOne(po => po.SaleOrder)
.OnDelete(DeleteBehavior.SetNull);
}
And as far the Entities, they are:
public abstract class Business : IEntity
{
protected Business()
{
CreatedOn = DateTime.UtcNow;
}
public int Id { get; set; }
public string Name { get; set; }
public string TaxNumber { get; set; }
public string Description { get; set; }
public string Phone { get; set; }
public string Website { get; set; }
public string Email { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public ICollection<Address> Addresses { get; set; } = new List<Address>();
public ICollection<Contact> Contacts { get; set; } = new List<Contact>();
}
[Table("Customers")]
public class Customer : Business
{
public decimal AllowedCredit { get; set; }
public decimal CreditUsed { get; set; }
public int NumberOfDaysAllowedToBeOnMaxedOutCredit { get; set; }
public ICollection<SaleOrder> SaleOrders { get; set; }
}
[Table("Suppliers")]
public class Supplier : Business
{
public ICollection<PurchaseOrder> PurchaseOrders { get; set; }
}
public abstract class Order : IEntity
{
protected Order()
{
Date = DateTime.UtcNow;
CreatedOn = DateTime.UtcNow;
}
public int Id { get; set; }
public DateTime Date { get; set; }
public decimal ShippingCost { get; set; }
public Currency ShippingCurrency { get; set; }
public decimal ShippingConversionRate { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public ICollection<Invoice> Invoices { get; set; }
public ICollection<Note> Notes { get; set; }
}
[Table("PurchaseOrders")]
public class PurchaseOrder : Order
{
public int SupplierOrderNumber { get; set; }
public PurchaseOrderStatus Status { get; set; }
public decimal Vat { get; set; }
public decimal ImportDuty { get; set; }
public int SupplierId { get; set; }
public Supplier Supplier { get; set; }
public ICollection<PurchaseOrderLineItem> LineItems { get; set; }
}
[Table("SaleOrders")]
public class SaleOrder : Order
{
public decimal AmountToBePaidOnCredit { get; set; }
public SaleOrderStatus Status { get; set; }
public ICollection<SaleOrderLineItem> LineItems { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
So after doing what Ahmar suggested, I still get the same error when I do update-database.
You need to configure cascade delete behavior on each entity in .Net Core EF.
The Entity Framework Core Fluent API OnDelete method is used to specify the action which should take place on a dependent entity in a relationship when the principal is deleted.
The OnDelete method takes a DeleteBehavior enum as a parameter:
Cascade - dependents should be deleted
Restrict - dependents are
unaffected
SetNull - the foreign key values in dependent rows should
update to NULL
Example:
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int? CompanyId { get; set; }
public Company Company { get; set; }
}
protected override void OnModelCreating(Modelbuilder modelBuilder)
{
modelBuilder.Entity<Company>()
.HasMany(c => c.Employees)
.WithOne(e => e.Company).
.OnDelete(DeleteBehavior.SetNull);
}
When deleting the Company, it will set CompanyId property in Employee table to null.
Get more detail at Configuring One To Many Relationships
PS. Please make sure your all referencing properties should be null able so, EF Core can set them null on delete. like CompanyId in about example.
This is an attempt at taking this question a bit further.
I am attempting to link a couple of objects. First, I would like to associate a Vertical with a Product. The vertical id will (eventually) be passed in through a constructor, or accessible through a linked object, but for right now I have attempted to manually set the VerticalID.l The code for all three objects is below:
public class Product
{
public int ID { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public string PartNumber { get; set; }
public int CategoryID { get; set; }
public string Description { get; set; }
public int VerticalID = 1;
public virtual Vertical Vertical { get; set; }
public virtual ProductCategory Category { get; set; }
public virtual ProductPrice Price { get; set; }
public virtual ICollection<ProductImage> Images { get; set; }
public virtual ICollection<ProductDocument> Documents { get; set; }
public virtual ICollection<ProductDetail> Details { get; set; }
public virtual ICollection<RelatedProduct> RelatedProducts { get; set; }
}
public class Vertical
{
public int ID { get; set; }
public string Name { get; set; }
}
public class ProductPrice
{
public int ID { get; set; }
public int ProductID { get; set; }
public int VerticalID { get; set; }
public decimal Value { get; set; }
public virtual Product Product { get; set; }
public virtual Vertical Vertical { get; set; }
public override string ToString()
{
return Value.ToString("C");
}
}
Once I have successfully linked a vertical. I would then like to link a Product to a ProductPrice using the ProductID and VerticalID foreign keys. Basically, the relationship is:
1 price per product per vertical.
I have tried linking a product and a vertical using the fluent api as recommended in one of the answers of my previous question (linked above), but have come up empty handed. Here was my attempt:
public class ApplicationContext : DbContext
{
public ApplicationContext() : base("DefaultConnection")
{
}
public DbSet<Product> Products { get; set; }
public DbSet<ProductCategory> Categories { get; set; }
public DbSet<ProductDetail> ProductDetails { get; set; }
public DbSet<ProductDocument> Documents { get; set; }
public DbSet<ProductImage> Images { get; set; }
public DbSet<ProductPrice> Prices { get; set; }
public DbSet<RelatedProduct> RelatedProducts { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Vertical> Verticals { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasRequired(a => a.Vertical)
.WithRequiredDependent();
}
}
How do I link a Product to a vertical using an attribute not mapped to a db column and (more importantly) how do I link a Product to a ProductPrice using 2 foreign keys?
Here is my model
public class Horse
{
public int HorseId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public LegType LegType { get; set; }
public Character Character { get; set; }
public int Hearts { get; set; }
public bool Retired { get; set; }
// Parents
public Horse Sire { get; set; }
public Horse Dam { get; set; }
// Internals
public int Stamina { get; set; }
public int Speed { get; set; }
public int Sharp { get; set; }
// Special
public int Dirt { get; set; }
// Externals
public int Start { get; set; }
public int Corner { get; set; }
public int OutOfTheBox { get; set; }
public int Competing { get; set; }
public int Tenacious { get; set; }
public int Spurt { get; set; }
//Races
public virtual ICollection<Race> RaceResults { get; set; }
//Training
public virtual ICollection<Training> TrainingResults { get; set; }
}
public class Race
{
public int RaceId { get; set; }
public int Favorite { get; set; }
public LegType LegType { get; set; }
public int Players { get; set; }
public DateTime Split { get; set; }
public DateTime Final { get; set; }
public int Position { get; set; }
public virtual int TrackId { get; set; }
public virtual Track Track { get; set; }
public virtual int LinkedHorseId { get; set; }
public virtual Horse LinkedHorse { get;set; }
}
public class Training
{
public int TrainingId { get; set; }
public string Type { get; set; }
public string Result { get; set; }
public string Food { get; set; }
public int Start { get; set; }
public int Corner { get; set; }
public int Outofthebox { get; set; }
public int Competing { get; set; }
public int Tenacious { get; set; }
public int Spurt { get; set; }
public virtual int LinkedHorseId { get; set; }
public virtual Horse LinkedHorse { get; set; }
}
public class Track
{
public int TrackId { get; set; }
public string Name { get; set; }
public int Distance { get; set; }
public bool G1 { get; set; }
public int Prize { get; set; }
}
And here is my fluent API code.
public class HorseTracker : DbContext
{
public DbSet<Horse> Horses { get; set; }
public DbSet<LegType> LegTypes { get; set; }
public DbSet<Character> Characters { get; set; }
public DbSet<Training> TrainingResults { get; set; }
public DbSet<Track> Tracks { get; set; }
public DbSet<Race> Races { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Race>()
.HasRequired(r => r.LinkedHorse)
.WithMany(h => h.RaceResults)
.HasForeignKey(r => r.LinkedHorseId);
modelBuilder.Entity<Training>()
.HasRequired(t => t.LinkedHorse)
.WithMany(t => t.TrainingResults)
.HasForeignKey(t => t.LinkedHorseId);
modelBuilder.Entity<Race>()
.HasRequired(r => r.Track)
.WithMany()
.HasForeignKey(r => r.TrackId)
.WillCascadeOnDelete(false);
}
}
I keep getting this error:
Unable to determine the principal end of an association between the types 'DOCCL.Models.Horse' and 'DOCCL.Models.Horse'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
Any clue what i'm doing wrong.
I've been playing around with no foreign keys. making one of the required lists optional.
they all result in different errors.
mostly saying that the relation needs to be a 1:1 relation.
And once it said that it had a non nullable field.
I made that nullable int? and then i got the first error again.
I think you need to setup self-referencing relationships manually (specifically, the Horse class properties Sire and Dam are causing an issue).
Try this (in the answer):
What is the syntax for self referencing foreign keys in EF Code First?
You could add two more int IDs representing the foreign keys (SireId, DamId).
If you add this to your model configuration it should work:
modelBuilder.Entity<Horse>()
.HasRequired(h => h.Dam) // or HasOptional
.WithMany();
modelBuilder.Entity<Horse>()
.HasRequired(h => h.Sire) // or HasOptional
.WithMany();
The problem is that the mapping conventions try to create a one-to-one relationship between Dam and Sire and therefore cannot determine what's the principal and what's the dependent because both are optional. Anyway I guess you don't want a one-to-one relationships but actually two one-to-many relationships (the many-side (the "children") not being exposed in the model).