C# Entity Framework использует вложенное свойство в качестве внешнего ключаC#

Место общения программистов C#
Гость
C# Entity Framework использует вложенное свойство в качестве внешнего ключа

Сообщение Гость »

Код: Выделить всё

public class User {
public int UserId { get; set; }

public ICollection Orders { get; set; }
public ICollection Sales { get; set; }
}

public class Listing {
public int ListingId { get; set; }

public int UserId { get; set; }
public User User { get; set; }
}

public class Order {
public int ListingId { get; set; }
public Listing Listing { get; set; }

public int BuyerId { get; set; }
public User Buyer { get; set; }
}

modelBuilder.Entity()
.HasOne(e => e.Buyer)
.WithMany(e => e.Orders)
.HasForeignKey(e => e.BuyerId);

modelBuilder.Entity()
.HasOne(e => e.Listing.User)
.WithMany(e => e.Sales)
.HasForeignKey(e => e.Listing.UserId);

I am having trouble configuring the relationship for the

Код: Выделить всё

User.Sales
property.
The code above should explain what I'm trying to do but the

Код: Выделить всё

e.Listing.User
and

Код: Выделить всё

e.Listing.UserId
parts do not work.
I know I can solve the problem by introducing

Код: Выделить всё

SellerId
and properties to the class but that feels like a waste because these are accessible through the property, specifically through

Код: Выделить всё

Order.Listing.UserId
and

Код: Выделить всё

Order.Listing.User
respectively.
Is there a way to configure the relationship without that?


Источник: https://stackoverflow.com/questions/781 ... oreign-key

Вернуться в «C#»