entity-framework

EF Core LINQ exclude column from included entity

百般思念 提交于 2020-04-07 06:38:52
问题 I'm using ASP.Net Core 2.0 with Entity Framework and I am trying to return a model to a page that contains the Employment entity with it's collection of EmploymentDocument entities also included. For the latter I do not want to load the data (byte[]) column but I do want all the other columns, most importantly FileName. The linq query I have which loads everything including the data column is: var employment = await _context.Employment .Include(e => e.EmploymentDocuments) // will load all

Converting EF Core queries from 2.2 to 3.0 - async await

女生的网名这么多〃 提交于 2020-04-07 04:01:04
问题 In EF Core 2.2 I had: var data = await _ArticleTranslationRepository.DbSet .Include(arttrans => arttrans.Article) .ThenInclude(art => art.Category) .Where(trans => trans.Article != null && trans.Article.Category != null && trans.Article.Category.Id == categoryId.Value) .GroupBy(trans => trans.ArticleId) .Select(g => new { ArticleId = g.Key, TransInPreferredLang = g.OrderByDescending(trans => trans.LanguageId == lang).ThenByDescending(trans => trans.LanguageId == defaultSiteLanguage).ThenBy

Converting EF Core queries from 2.2 to 3.0 - async await

坚强是说给别人听的谎言 提交于 2020-04-07 03:58:16
问题 In EF Core 2.2 I had: var data = await _ArticleTranslationRepository.DbSet .Include(arttrans => arttrans.Article) .ThenInclude(art => art.Category) .Where(trans => trans.Article != null && trans.Article.Category != null && trans.Article.Category.Id == categoryId.Value) .GroupBy(trans => trans.ArticleId) .Select(g => new { ArticleId = g.Key, TransInPreferredLang = g.OrderByDescending(trans => trans.LanguageId == lang).ThenByDescending(trans => trans.LanguageId == defaultSiteLanguage).ThenBy

Best way to override SaveChanges()

被刻印的时光 ゝ 提交于 2020-04-04 13:08:52
问题 We have worked on a project for 1 month and there are 6 entities without any relationship with other entities. They are all simple entities. We have created 6 different classes for operations on each entity. SaveOrUpdateEntity() methods of classes are almost same as you think. It is something like that: public static ErrorType SaveOrUpdateEntity(Entity entity, int userID) { try { using (DataEntities ctx = new DataEntities()) { if (entity != null) { if (entity.entityID == 0) { entity

Enity Framework With MySQL

瘦欲@ 提交于 2020-04-01 08:18:33
问题 I am getting the following error "The Entity Framework provider type 'MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity' registered in the application config file for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information." However I do have MySql.Data.dll and MySql

Assign Mapped Object to Expression Result in LINQ to Entities

若如初见. 提交于 2020-03-25 19:19:20
问题 I have the following child object that we use an expression to map our 'entity' to our 'domain' model. We use this when specifically calling our ChildRecordService method GetChild or GetChildren: public static Expression<Func<global::Database.Models.ChildRecord, ChildRecord>> MapChildRecordToCommon = entity => new ChildRecord { DateTime = entity.DateTime, Type = entity.Type, }; public static async Task<List<ChildRecord>> ToCommonListAsync(this IQueryable<global::Database.Models.ChildRecord>

What is a correct way to select a property of optional navigation property in Entity Framework?

可紊 提交于 2020-03-25 16:12:52
问题 What is a correct way to select a property of optional navigation property entity framework? I am concerned that in case the navigation property will be null, then the error will be thrown when I try to access its (optional navigation property`s) property. Here is what I tried: return await this.relatedCasesRepository .GetAll() .AsNoTracking() .Where(rc => rc.FirstCaseId == caseId || rc.SecondCaseId == caseId) .Select(rc => new RelatedCaseInfoDto { FirstCaseId = rc.FirstCaseId, FirstCaseName

Get Dictionary value in IQueryable LINQ to Entities query

时间秒杀一切 提交于 2020-03-25 12:33:24
问题 I have to support multiple languages in production application. There are lot of Entity Framework queries that gets data from database as deferred IQueryable list like this: public IQueryable<Request> GetDeferredRequests() { return _dbContext.Set<Request>(); } POCO class looks like this: public partial class Request { public int RequestID { get; set; } public string StatusName { get; set; } public string RequestType { get; set; } } Data Transfer Object looks like this: public class RequestDTO

where is DbContextTransaction in .Net Core

假装没事ソ 提交于 2020-03-24 00:06:18
问题 I need to use DbContextTransaction in a class on a .Net Standard library, I haven't been able to find the corresponding nuget package, I am porting this https://github.com/mehdime/DbContextScope/tree/master/Mehdime.Entity to .Net Core, but I only have 2 compilation errores due to missing packages/dependencies which I am not sure how to find: Error CS0246: The type or namespace name 'MarshalByRefObject' could not be found (are you missing a using directive or an assembly reference?) (CS0246)

Why do navigation properties have to be public for a proxy to be created?

扶醉桌前 提交于 2020-03-22 08:04:26
问题 At http://msdn.microsoft.com/en-us/library/dd468057.aspx I read that all navigation properties for which I would like to have a change tracking proxy need to be public and virtual . From what I understand, the CLR creates subclasses of my POCOs dynamically, and it re-implements the properties to provide the requested behaviour. For this I undertand that the property needs to be virtual , and that it should have protected or higher accesability. However, if I want to use these for convenience