Reputation: 31
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; }
}
Upvotes: 0
Views: 438
Reputation: 971
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; }
}
Upvotes: 3
Reputation: 791
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
Upvotes: 1
Reputation: 36715
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
Upvotes: 1