Sto creando un progetto Entity Framework 7 per sostituire un progetto Entity Framework 6.Proprietà navigazione Entity Framework 7 è nullo
Ho un'entità Articolo che appartiene a un paese. Ho quindi una query linq che ottiene il conteggio per paese. Ecco la query.
var results = allItems
.GroupBy(g => g.Country)
.ToDictionary(s => s.Key, s => s.Count());
Questo funziona con EF6 ma genera un'eccezione con EF 7 (l'eccezione è in fondo).
Questo è il mio soggetto:
public class Item
{
[Key]
public int id { get; set; }
[Required]
public int Countryid { get; set; }
[ForeignKey("Countryid")]
public virtual Country Country { get; set; }
}
In EF 7, con il debugger vedo che il Paese è nullo (che è la proprietà di navigazione), ma io ho il countryID. In EF 6, ho un oggetto per la proprietà di navigazione. Inoltre, ho dei test unitari usando Moq e funzionano (mostra la proprietà nav).
Ho provato ad aggiungere un inclusione ma non avrei dovuto averne bisogno. Non ne avevo bisogno in EF 6 o con il Mock.
Utilizzando includere dà questo:
var results = allItems
.Include(i => i.Country)
.GroupBy(g => g.Country)
.ToDictionary(s => s.Key, s => s.Count());
ottengo lo stesso errore.
Ecco l'errore:
Expression of type 'System.Func
2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier
2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier2[FMS.DAL.Entities.ActionItem,Microsoft.Data.Entity.Storage.ValueBuffer],Microsoft.Data.Entity.Storage.ValueBuffer],FMS.DAL.Entities.MemberCountry]' cannot be used for parameter of type 'System.Func
2[FMS.DAL.Entities.ActionItem,FMS.DAL.Entities.MemberCountry]' of method 'System.Collections.Generic.IEnumerable1[System.Linq.IGrouping
2[FMS.DAL.Entities.MemberCountry,FMS.DAL.Entities.ActionItem]] _GroupBy[ActionItem,MemberCountry,ActionItem](System.Collections.Generic.IEnumerable1[FMS.DAL.Entities.ActionItem], System.Func
2[FMS.DAL.Entities.ActionItem,FMS.DAL.Entities.MemberCountry], System.Func`2[FMS.DAL.Entities.ActionItem,FMS.DAL.Entities.ActionItem])'