简体   繁体   English

实体框架核心与多对多关系

[英]Entity Framework Core and many to many relation

I try to migrate a small application to Entity Framework Core but I cant get the many to many relation to work. 我尝试将一个小型应用程序迁移到Entity Framework Core,但无法获得很多对很多的关系。 First my Entities 首先我的Entities

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; }
}

and my DbContext is 而我的DbContext是

modelBuilder.Entity().HasKey(currency => new { currency.CountryId, currency.CurrencyId }); modelBuilder.Entity()。HasKey(currency => new {currency.CountryId,currency.CurrencyId}); modelBuilder.Entity() .HasOne(pt => pt.Country) .WithMany(p => p.CountryCurrencies) .HasForeignKey(pt => pt.CountryId); modelBuilder.Entity().HasOne(pt => pt.Country).WithMany(p => p.CountryCurrencies).HasForeignKey(pt => pt.CountryId);

  modelBuilder.Entity<CountryCurrency>() .HasOne(pt => pt.Currency) .WithMany(t => t.CountryCurrencies) .HasForeignKey(pt => pt.CurrencyId); 

now when I add a currency for example 现在,例如当我添加货币时

  Currency currency; Country country; CountryCurrency countryCurrency; currency = new Currency(); currency.Id = i++; currency.User = null; currency.Code = "ETB"; currency.Name = "Ethiopian Birr"; currency.Symbol = "Br"; country = this._context.Countries.FirstOrDefault( country1 => country1.Iso2Code == "ET"); if (country != null) { currency.CountryCurrencies = new List<CountryCurrency>(); countryCurrency = new CountryCurrency(); countryCurrency.Country = country; countryCurrency.Currency = currency; currency.CountryCurrencies.Add(countryCurrency); this.InitialCurrencies.Add(currency); } this._context.Currencies.Add(currency); 

so when now I'm retrieve the data in my test I get this with this code 所以现在当我在测试中检索数据时,我会通过以下代码获得此信息

Country = context.Countries.Include(country => country.CountryCurrencies).First();

调试器

I can't get the currency the id is set but the property not... 我无法获得设置了ID的货币,但该属性却没有...

You have also to include the currency entity, not just the join entity 您还必须包括货币实体,而不仅仅是联接实体

Country = context.Countries
    .Include(country => country.CountryCurrencies)
        .ThenInclude(e => e.Currency)
    .First();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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