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; }
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With