Domanda

Voglio costruire un grafico oggetto usando il quadro di entità 4.2.

In questo momento, ho entità Poco, utilizzando iCollezione per le proprietà di navigazione. Voglio evitare di usare EntityCollection o qualsiasi cosa specifica per EF.

Voglio evitare che i magnifici unici causati dall'uso includono eccessivamente. Dato un oggetto, voglio compilare le sue proprietà di navigazione, con conseguente una query separata del database.

C'è un modo per popolare direttamente un iCollezione? In questo momento, sto lavorando attorno al problema, ma è davvero doloroso.

// grab the user, brand users and brands
User user = entities.Users
                    .Include(item => item.BrandUsers.Select(brandUser => brandUser.Brand))
                    .Where(item => item.Name == userName)
                    .SingleOrDefault();
// grab the pending share grants and brands
entities.Users
        .Include(item => item.ToShareGrants.Select(shareGrant => shareGrant.Brand))
        .Where(item => item.Id == user.Id)
        .Load();
return user;
.

Una cosa che non mi piace di questo approccio è che sto riconterrendo l'oggetto di alto livello. Se non lo faccio, la proprietà di navigazione non è popolata (sinistra null) quando non ci sono oggetti restituiti. Ad esempio, il seguente codice solo funziona se i risultati vengono restituiti:

entities.ShareGrants
        .Include(item => item.Brand)
        .Where(item => item.ToUserId == user.Id)
        .Load();
.

Sono stato curioso se c'era solo un metodo non ero a conoscenza del quadro di entità per la costruzione di questi tipi di relazioni. Se qualcuno conosce un approccio facile per compilare le proprietà di navigazione nei passaggi, apprezzerei un campione di codice.

È stato utile?

Soluzione 3

The short answer to this question is that EF4 did not directly support the functionality I wanted. In order to prevent a massive join and break the results out across multiple calls to the database, the top-most entity must be downloaded from the database again. This means the left-most columns in the result set will be the columns of that entity repeated for each record.

Altri suggerimenti

Try turn off lazy loading for your current query, you may just put this in a using block

entities.ContextOptions.LazyLoadingEnabled = false;

Your question is not very clear. Why not use multiple Includes in the same query

User user = entities.Users
      .Include(item => item.BrandUsers.Select(brandUser => brandUser.Brand))
      .Include(item => item.ToShareGrants.Select(shareGrant => shareGrant.Brand))
      .Where(item => item.Name == userName)
      .SingleOrDefault();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top