2016-01-21 3 views
6

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+TransparentIdentifier 2[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.IEnumerable 1[System.Linq.IGrouping 2[FMS.DAL.Entities.MemberCountry,FMS.DAL.Entities.ActionItem]] _GroupBy[ActionItem,MemberCountry,ActionItem](System.Collections.Generic.IEnumerable 1[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])'

risposta

1

Attualmente GroupBy non è implementata in EF7 lo stato delle funzioni possono essere trovate sulla pagina mappa stradale qui. https://github.com/aspnet/EntityFramework/wiki/Roadmap

un lavoro intorno potrebbe essere:

context.Countries.Select(x => new 
{ 
    x.Id, 
    Items = x.Items.Count 
}).ToDictionary(x => x.Id, x => x.Items); 

public class Country 
{ 
    public int Id { get; set; } 

    //Add this property 
    public virtual ICollection<Item> Items { get; set; } 
} 

//Generated SQL 
SELECT (
    SELECT COUNT(*) 
    FROM [Item] AS [i] 
    WHERE [x].[Id] = [i].[Countryid] 
), [x].[Id] 
FROM [Country] AS [x] 

Ciò richiederebbe l'aggiunta di una proprietà Items per paese, ma permetterebbe di raggiungere quello che sono dopo tutto in LINQ. Si potrebbe anche andare a scrivere la query in sql ed eseguire con EF ma potrebbe non essere l'opzione migliore.