ุฎู„ูŠู†ุง ู†ูƒู…ู„ ูˆู†ุดูˆู ุฅุฒุงูŠ ู†ุณุชุฎุฏู… ุงู„ู€ Fluent API ู„ูƒู„ Entity ุนู†ุฏูƒ ููŠ ุงู„ู€ Order ModuleุŒ ูˆู†ูƒุชุจ ุงู„ู€ Configuration Class ู„ูƒู„ ูˆุงุญุฏุฉ ู…ู†ู‡ู…: Order, OrderItems, ูˆDeliveryMethod.

Order Config

  • ู‡ู†ุจุฏุฃ ุจุนู…ู„ Configuration Class ู„ู„ู€ Order Entity.
  • ู‡ู†ุฑูˆุญ ููŠ ุงู„ู€ Repository ูˆููŠ ููˆู„ุฏุฑ ุงู„ู€ Config ูˆู†ุนู…ู„ class ุงุณู…ู‡ OrderConfigurations ูˆู†ุณูŠุจู‡ Internal.
  • ู‡ู†ุณุชุฎุฏู… ุงู„ู€ Fluent API ู„ุชุญุฏูŠุฏ ุงู„ุนู„ุงู‚ุงุช ูˆุงู„ุฎุตุงุฆุต.
internal class OrderConfigurations : IEntityTypeConfiguration<Order>
{
	public void Configure(EntityTypeBuilder<Order> builder)
	{
		// One-to-One relationship: Shipping Address
		builder.OwnsOne(O => O.ShippingAddress, shippingAddress => shippingAddress.WithOwner());
 
		// Enum: Order Status
		builder.Property(O => O.Status)
			.HasConversion(
				OStatus => OStatus.ToString(),
				OStatus => (OrderStatus) Enum.Parse(typeof(OrderStatus), OStatus)
			);
 
		// Relationship with Delivery Method
		builder.HasOne(O => O.DeliveryMethod)
			.WithMany(); // Default
 
		// Subtotal as decimal
		builder.Property(O => O.Subtotal)
			.HasColumnType("decimal(18,2)");
	}
}

ุฎู„ูŠู†ุง ู†ุดุฑุญ ุงู„ุฌุฒุก ุงู„ู…ุชุนู„ู‚ ุจุงู„ู€ Fluent API ุงู„ู…ุณุชุฎุฏู… ููŠ ุงู„ุชุนุงู…ู„ ู…ุน ุงู„ู€ Delivery Method ูˆุงู„ู€ Subtotal ููŠ ุงู„ู€ Order Entity:

Delivery Method Relationship

ุงู„ุนู„ุงู‚ุฉ ุจูŠู† ุงู„ู€ Order ูˆุงู„ู€ Delivery Method ู‡ูŠ ููŠ ุงู„ุฃุณุงุณ One-to-Many ู…ู† ู†ุงุญูŠุฉ ุงู„ู€Order. ูŠุนู†ูŠ ู…ู…ูƒู† ูŠูƒูˆู† ุนู†ุฏูƒ ุฃูƒุชุฑ ู…ู† Order ุจูŠุณุชุฎุฏู… ู†ูุณ ุงู„ู€ Delivery Method. ู„ูƒู† ู„ูˆ ุนุงูŠุฒ ุชุถู…ู† ุฅู†ู‡ุง One-to-OneุŒ ู…ู…ูƒู† ุชุนู…ู„ Constraint ุจุงุณุชุฎุฏุงู… ุงู„ู€ Fluent API.

  1. Default Relationship:

    builder.HasOne(O => O.DeliveryMethod)
        .WithMany(); // Default
    • ู‡ู†ุง ุจู†ุญุฏุฏ ุฅู† ุงู„ู€ Order ุนู†ุฏู‡ DeliveryMethodุŒ ูˆุงู„ุนู„ุงู‚ุฉ ุงู„ุงูุชุฑุงุถูŠุฉ ู‡ูŠ One-to-Many ู…ู† ู†ุงุญูŠุฉ ุงู„ู€ Order. ูŠุนู†ูŠ ู…ู…ูƒู† ุฃูƒุชุฑ ู…ู† Order ูŠุณุชุฎุฏู… ู†ูุณ ุงู„ู€ Delivery Method.
  2. One-to-One Constraint:

    builder.HasOne(O => O.DeliveryMethod)
        .WithOne();
    • ู„ูˆ ุนุงูŠุฒ ุชุถู…ู† ุฅู† ุงู„ุนู„ุงู‚ุฉ One-to-OneุŒ ู…ู…ูƒู† ุชุณุชุฎุฏู… WithOne() ู„ุชุญุฏูŠุฏ ุฅู† ูƒู„ Order ู„ุงุฒู… ูŠูƒูˆู† ู„ู‡ Delivery Method ูˆุงุญุฏ ูู‚ุทุŒ ูˆุงู„ุนูƒุณ ุตุญูŠุญ.
  3. Unique Constraint:

    builder.HasIndex(O => O.DeliveryMethodId).IsUnique();
    • ู‡ู†ุง ุจู†ุนู…ู„ Unique Constraint ุนู„ู‰ ุงู„ู€ Foreign Key ุจุชุงุน ุงู„ู€ Delivery Method ููŠ ุงู„ู€ Order. ุฏู‡ ุจูŠุถู…ู† ุฅู† ูƒู„ Order ู„ู‡ Delivery Method ูุฑูŠุฏุŒ ู„ูƒู† ู…ุด ุถุฑูˆุฑูŠ ุชุนู…ู„ ุฏู‡ ู„ูˆ ู…ุด ู…ุญุชุงุฌู‡.

Subtotal Property

ุงู„ู€ Subtotal ู‡ูˆ ู‚ูŠู…ุฉ ู…ู† ู†ูˆุน decimalุŒ ูˆุนู„ุดุงู† ู†ุญุฏุฏ ุฅุฒุงูŠ ู‡ูŠุชุฎุฒู† ููŠ ุงู„ู€ DatabaseุŒ ุจู†ุณุชุฎุฏู… ุงู„ู€ Fluent API ู„ุชุญุฏูŠุฏ ู†ูˆุน ุงู„ุนู…ูˆุฏ.

builder.Property(O => O.Subtotal)
    .HasColumnType("decimal(18,2)");
  • ุงู„ู€ HasColumnType("decimal(18,2)"): ุฏู‡ ุจูŠุญุฏุฏ ุฅู† ุงู„ุนู…ูˆุฏ ุงู„ู„ูŠ ู‡ูŠุฎุฒู† ุงู„ู€ Subtotal ููŠ ุงู„ู€ Database ู‡ูŠูƒูˆู† ู…ู† ู†ูˆุน decimal ู…ุน 18 ุฑู‚ู… ุฅุฌู…ุงู„ูŠ ูˆ2 ุฑู‚ู… ุจุนุฏ ุงู„ุนู„ุงู…ุฉ ุงู„ุนุดุฑูŠุฉ. ุฏู‡ ุจูŠุณุงุนุฏ ููŠ ุชุฎุฒูŠู† ุงู„ู‚ูŠู… ุงู„ู…ุงู„ูŠุฉ ุจุฏู‚ุฉ ุนุงู„ูŠุฉ.

ู…ู„ุงุญุธุงุช ุฅุถุงููŠุฉ

  • ุงุณุชุฎุฏุงู… ุงู„ู€ HasColumnType ู…ู‡ู… ุฌุฏู‹ุง ููŠ ุงู„ุชุนุงู…ู„ ู…ุน ุงู„ู‚ูŠู… ุงู„ู…ุงู„ูŠุฉ ุนุดุงู† ุชุชุฌู†ุจ ุฃูŠ ู…ุดุงูƒู„ ููŠ ุงู„ุฏู‚ุฉ ุนู†ุฏ ุงู„ุชุฎุฒูŠู† ููŠ ุงู„ู€ Database.
  • ู„ูˆ ู…ุด ู…ุญุชุงุฌ ุชุนู…ู„ Unique Constraint ุนู„ู‰ ุงู„ู€ Delivery MethodุŒ ู…ู…ูƒู† ุชูƒุชููŠ ุจุงู„ุนู„ุงู‚ุฉ ุงู„ุงูุชุฑุงุถูŠุฉ One-to-Many.

Delivery Method Config

  • ุงู„ุนู„ุงู‚ุฉ ุจูŠู† ุงู„ู€ Order ูˆุงู„ู€ Delivery Method ู‡ูŠ One-to-Many ู…ู† ู†ุงุญูŠุฉ ุงู„ู€ Order.
  • ู…ู…ูƒู† ู†ุนู…ู„ Constraint ู„ูˆ ุนุงูŠุฒูŠู† ู†ุถู…ู† ุฅู†ู‡ุง One-to-One.

Order Item Config

ู‡ู†ุนู…ู„ Configuration Class ู„ู„ู€ OrderItem Entity.

internal class OrderItemConfiguration : IEntityTypeConfiguration<OrderItem>
{
	public void Configure(EntityTypeBuilder<OrderItem> builder)
	{
		// One-to-One relationship: ProductItemOrdered
		builder.OwnsOne(orderItem => orderItem.Product, Product => Product.WithOwner());
 
		// Price as decimal
		builder.Property(orderItem => orderItem.Price)
			.HasColumnType("decimal(18,2)");
	}
}

Apply Configs

ุนู„ุดุงู† ู†ุทุจู‚ ูƒู„ ุงู„ู€ Configurations ุงู„ู„ูŠ ุนู…ู„ู†ุงู‡ุงุŒ ู‡ู†ุฑูˆุญ ู„ู„ู€ StoreContext ูˆู†ุนู…ู„ Apply ู„ูƒู„ ุงู„ู€ Configurations.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
	modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}

ู…ู„ุงุญุธุงุช ุฅุถุงููŠุฉ

  • ุชุฃูƒุฏ ุฅู†ูƒ ุจุชุณุชุฎุฏู… ุงู„ู€ namespace ุงู„ุตุญูŠุญ ู„ูƒู„ Entity ูˆEnum ุนุดุงู† ุชุชุฌู†ุจ ุฃูŠ ุชุนุงุฑุถุงุช.
  • ู„ูˆ ุนุงูŠุฒ ุชุนู…ู„ Unique Constraint ุนู„ู‰ ุงู„ู€ Delivery Method ููŠ ุงู„ู€ OrderุŒ ู…ู…ูƒู† ุชุณุชุฎุฏู… HasIndex ู…ุน IsUnique().