ef-core-3.0

Entity Framework Core throws System.Data.SqlTypes.SqlNullValueException when loading entities from DbContext

天涯浪子 提交于 2020-06-01 05:10:22
问题 I have this MyEntity table in the database: The data inside MyEntity is the following: The EF Core entity is the following: public class MyEntity { public int Id { get; set; } public int MyInt { get; set; } } I am getting an exception: System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.' when trying to load the MyEntity from the DbContext : var myEntities = dbContext.Set<MyEntity>.ToList(); What am I doing wrong? 回答1: You are

EF Core unable to determine relationship represented by foreign keys in abstract class

可紊 提交于 2020-05-30 08:47:10
问题 I'm trying to implement an abstract class for CRUD objects which contains two references to the User class; one for the User which created the object and the other for the User which last modified it. Entity framework is unable to determine the relationship represented by the CreatedBy and ModifiedBy navigation properties on the class inheriting them (Department). A potential additional complication is that the User class also has a property of class Department, which is unrelated to the

EF Core 3 DbQuery equivalent functionality

*爱你&永不变心* 提交于 2020-05-15 19:12:09
问题 In ef core 2.2 I have used DbQuery to map raw sql results to object as following: public partial class AppDbContext{ public DbQuery<SimpleQueryModel> SimpleQM {get;set;} } and then var result=_dbContext.SimpleQM.FromSql(sqlString,params); this wouldn't create any extra table and working just fine. In ef core 3.1 DbQuery is obsolete and telling me to use keyless DbSet instead. I have configured it as following: public partial class AppDbContext{ public DbSet<SimpleQueryModel> SimpleQM {get;set

Simplest Group By Fails in EF 3.x with “Client side GroupBy is not supported”

会有一股神秘感。 提交于 2020-05-15 07:52:07
问题 Currently testing with EF Core version 3.1.1. When I search, I find references to this problem, but I don't see any definitive answers about why this happens, and whether it is a bug that will be fixed or if it's expected behavior. This is just as simple as it looks--the BatchRequest table/entity has an integer column/property called BatchId: var batchRequestGroups = context.BatchRequests.GroupBy(br => br.BatchId).ToList(); When I run this, I get a System.InvalidOperationException, "Client

Simplest Group By Fails in EF 3.x with “Client side GroupBy is not supported”

我们两清 提交于 2020-05-15 07:51:19
问题 Currently testing with EF Core version 3.1.1. When I search, I find references to this problem, but I don't see any definitive answers about why this happens, and whether it is a bug that will be fixed or if it's expected behavior. This is just as simple as it looks--the BatchRequest table/entity has an integer column/property called BatchId: var batchRequestGroups = context.BatchRequests.GroupBy(br => br.BatchId).ToList(); When I run this, I get a System.InvalidOperationException, "Client

Returning Domain Objects from Repositories on Joining tables

元气小坏坏 提交于 2020-04-30 11:22:21
问题 I have been reading that Repositories should return domain objects only. I am having difficulty with implementing this. I currently have API with Service Layer, Repository and I am using EF Core to access sql database. If we consider User(Id, Name, address, PhoneNumber, Email, Username) and Orders (id, OrderDetails, UserId) as 2 domain objects. One Customer can have multiple Orders. I have created navigation property public virtual User User{ get; set; } and foreign Key. Service layer needs

.Net core 3.x Keyless Entity Types avoid table creation

夙愿已清 提交于 2020-04-07 05:11:32
问题 I need to execute a complex sql query in entity framework core 3.1.1, on researching i found out that keyless entity types is the way to go in code first approach. I see lot of documents for dbquery but this is marked as obsolete in .net core 3.x keyless entity types As per Microsoft documentation it says dbquery is obsolete so use dbset approach instead, but with dbset it is trying to create a new table in database. how to disable table generation in keyless entity types while applying

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

Sum of hours (HH:MM) in Linq

微笑、不失礼 提交于 2020-02-24 06:13:34
问题 I am having "TimeClocked" in my database table in the format HH.mm and I want to sum all the "TimeClocked" using LINQ. I have tried this aggregate function. var data = _projectDbContext .Tasks .Where(x => x.Project.CompanyId == companyId && x.Status == true) .Select(e => TimeSpan.Parse(e.TimeClocked)) .Aggregate(TimeSpan.FromMinutes(0), (total, next) => total + next) .ToString(); I am using EF Core 3.0.0 . It's showing error like this. Processing of the LINQ expression 'Aggregate<TimeSpan,