C# 实体框架核心和多对多关系
我尝试将一个小应用程序迁移到实体框架核心,但我无法使多对多关系正常工作。 首先是我的C# 实体框架核心和多对多关系,c#,entity-framework,asp.net-core,entity-framework-core,C#,Entity Framework,Asp.net Core,Entity Framework Core,我尝试将一个小应用程序迁移到实体框架核心,但我无法使多对多关系正常工作。 首先是我的实体 public class Currency : Entity<int>, IMayHaveUser { public string Code { get; set; } public string Name { get; set; } public string Symbol { get; set; } public virtual List<Country
实体
public class Currency : Entity<int>, IMayHaveUser
{
public string Code { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }
public virtual List<CountryCurrency> CountryCurrencies { get; set; }
public bool IsUserDefined => User != null;
[ForeignKey("UserId")]
public virtual User User { get; set; }
public long? UserId { get; set; }
}
public class Country : Entity<int>, IMayHaveUser
{
public string Iso2Code { get; set; }
public virtual ICollection<Era> Eras { get; set; }
public string Name { get; set; }
public virtual List<CountryCurrency> CountryCurrencies { get; set; }
[NotMapped]
public bool IsUserDefined => User != null;
[ForeignKey("UserId")]
public virtual User User { get; set; }
public long? UserId { get; set; }
}
public class CountryCurrency : Entity<Guid>
{
public int CountryId { get; set; }
public Country Country { get; set; }
public int CurrencyId { get; set; }
public Currency Currency { get; set; }
}
我无法获取货币id已设置,但属性不…您还必须包括货币实体,而不仅仅是连接实体
Country = context.Countries
.Include(country => country.CountryCurrencies)
.ThenInclude(e => e.Currency)
.First();
您还必须包括货币实体,而不仅仅是联接实体
Country = context.Countries
.Include(country => country.CountryCurrencies)
.ThenInclude(e => e.Currency)
.First();
Country = context.Countries.Include(country => country.CountryCurrencies).First();
Country = context.Countries
.Include(country => country.CountryCurrencies)
.ThenInclude(e => e.Currency)
.First();