简体   繁体   中英

How do I set up a one-to-many relationship using Entity Framework Core?

I'm working on a trucking API using Entity Framework (EF) Core. Basic CRUD operations are working fine using the repository pattern. There is an error in configurations I am implementing, however.

请在图片中找到错误

I want to obtain multiple trailers and trucks associated with single load, reflecting the one-to-many relationship.

public class LoadConfiguration : IEntityTypeConfiguration<Load>
{
    public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<Load> builder)
    {
        builder.Property(p=>p.Id).IsRequired();
        builder.HasOne(t=>t.Customer).WithMany().HasForeignKey(p=>p.CustomerId);
        builder.Property(p=>p.LoadedFrom).IsRequired();
        builder.HasMany(p=>p.Trailer).WithOne().HasForeignKey(t=>t.TrailerId); 
        builder.HasMany(p=>p.Truck).WithOne().HasForeignKey(t=>t.TruckId); 
        builder.Property(p=>p.Destination).IsRequired();
    }
}

public class Truck:BaseEntity
{
    public int PlateNo { get; set; }
    public string ModelName { get; set; }
    public Location StateCode { get; set; }
    public int PollutionCertificateValidity { get; set; }
    public int DateOfPurchase { get; set; }
    public int FitnessCertificateValidity { get; set; }
}

public class Load:BaseEntity
{
    public Customer Customer { get; set; }
    public int CustomerId { get; set; }
    public string LoadedFrom { get; set; }
    public Trailer Trailer { get; set; }
    public int TrailerId { get; set; }
    public Truck Truck { get; set; }
    public int TruckId { get; set; }
    public string Destination { get; set; }
}

public class Trailer:BaseEntity
{
    public int TrailerCapacity { get; set; }
    public Truck Truck { get; set; }
    public int TruckId { get; set; }
}

public class BaseEntity
{
    public int Id { get; set; }
}

A one-to-many relationship is defined by using navigation collections, that has the capacity to hold many Trucks and Trailers. You can choose the collection type freely, but I would suggest ICollection generic type.

Modify your Load class as follows:

public class Load:BaseEntity
{
    public Customer Customer { get; set; }
    public int CustomerId { get; set; }

    public string LoadedFrom { get; set; }

    public string Destination { get; set; }

    // navigation collections
    public ICollection<Trailer> Trailers { get; set; }
    public ICollection<Truck> Trucks { get; set; }
}

You will then be able to set up the relationship in your LoadConfiguration class by using the pluralized name:

builder.HasMany(p=>p.Trailers).WithOne();
builder.HasMany(p=>p.Trucks).WithOne();

.. even though EF Core will be smart enough to figure out the relation by convention so the fluent configuration is redundant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM