Define Entity Framework relationships using foreign keys only by FluentAPI

.net c# ef-fluent-api entity-framework entity-framework-core

Question

Is any way of defining Entity Framework relations using foreign keys only (no virtual properties of reference type) with FluentAPI (data models should not be changed)?

CardDataModel

public class CardDataModel
{
    public int CardId { get; set; }

}

CheckItemDataModel

public class CheckItemDataModel
{
    public int CheckItemId { get; set; }
    public int CardId { get; set; }
}
1
3
9/1/2017 12:04:41 PM

Accepted Answer

Yes, it's possible in EF Core. It wasn't in EF6 and below, but now EF Core provides parameterless overloads of HasMany / HasOne which allow configuring such relationship:

modelBuilder.Entity<CardDataModel>()
    .HasMany<CheckItemDataModel>() // <-- note this
    .WithOne()
    .HasForeignKey(e => e.CardId);
6
9/1/2017 1:47:38 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow