ef-core-2.0

EF Core 2 GroupBy Count predicate ignored

情到浓时终转凉″ 提交于 2019-12-11 05:02:46
问题 I currently have the following code: var result = query<Items>() .Where(x => x.Id == someId) .SelectMany(x => x.SubItems) .GroupBy(x => x.SubItemId) .Select(x => new ItemModel { SubItemId = x.Key, SpecialItemCount = x.Where(y => y.IsSpecial == false).Count(), }) .ToList()... When I call "Count()" it queries all subitem count ignoring my predicate "y.IsSpecial". I tried that on EF Core 2.0.X and EF Core 2.1 preview 2, is that possibly a bug? 回答1: You need to upgrade to EF Core 2.1 where

How to generate and auto increment Id with EF Core Oracle

こ雲淡風輕ζ 提交于 2019-12-11 02:54:11
问题 I use entity framework core 2 Code first to generate data in Oracle 11g, the entity provider is dotConnect, my POCO class is as follow: public partial class Favoritepage { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int Personnelid { get; set; } public int Pageid { get; set; } } My DbContext is as follow: public partial class ModelContext : DbContext { public virtual DbSet<Favoritepage> Favoritepage { get; set; } protected override void

EF Core change tracking - issue with original values and altered values

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 02:06:59
问题 I have Net core API configured with .net core 2.0 and EF core 2.0. it contains repository pattern architecture. Now, I am trying to implement Audit log for each save change using EF change tracker. My Issue : Whenever I tries to add a log for edit/modification endpoint, the original value and current value remain same and it's newly updated value. so in that way I am not able to track the modification or a change. Here is my ApplicationContext file where I have overridden save call. public

Entity Framework Core Slow First Time Loading

杀马特。学长 韩版系。学妹 提交于 2019-12-10 17:25:17
问题 I am dealing with Entity Framework Core DbContext Warm Up time. I have very large DbContext, and I am registering out DbContexes with AddDbContextPool() option, which register DbContext as a singleton and application reuses it. in an earlier version of EF, to speed up your application startup time following workarrounds are applied: Using a Cached DbModelStore, Generate pre-compiled Views, and/or Generate pre-compiled version of entity framework using NGen to avoid jitting. However, I dont

Why my Entity Framework creates several queries instead of a single UNION query?

独自空忆成欢 提交于 2019-12-10 16:20:24
问题 I use Entity Framework Core 2.0.1. In my EF query I need to combine several simple queries into a single UNION query. So, for a very basic example, I have a couple of similar queries using (var dbc = new ItemsDbContext("some-connection-string")) { var q1 = dbc.Items.Where(a => a.ItemType == "A"); var q2 = dbc.Items.Where(a => a.ItemType == "B"); var myList = q1.Union(q2).ToList(); } I would like such SQL statement to be executed, as a single query, against my SQL Server database: SELECT I.*

Ef Core 2 set value to ignored property on runtime

喜你入骨 提交于 2019-12-10 11:41:43
问题 I have an entity with boolean property named "ReadOnly", the value of this property depends on which user is using the application. In the DbContext i configured the property to be ignored. public class MyDbContext : DbContext { protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Entity>().Ignore(e => e.ReadOnly); } } How can i set the correct value calculated on runtime so that i haven't to remind to calculate the property everytime? EDIT: I was thinking

EF Core - navigational property in index

随声附和 提交于 2019-12-08 20:50:26
问题 I have the following two classes public class Tip { public string Home { get; set; } public string Away { get; set; } public string Prediction { get; set; } public Tipster Tipster { get; set; } ... other properties } public class Tipster { public int Id { get; set; } public string Username { get; set; } public string Platform { get; set; } } Now, I want to make unique index in theTip table. According to the EF Core documentation, there is no Data Annotations syntax, so I am using the fluent

Implementing Cascade Delete in a self referencing table in EF Core 2

柔情痞子 提交于 2019-12-08 05:39:45
问题 How can I implement Cascade Delete in a self referencing table in EF Core 2 (code first)? (For example there is a Comment Table and man can reply to a comment and this reply can reply by another.) public class Comment { public virtual int Id { get; set; } public virtual int? ParentId { get; set; } public Comment Parent { get; set; } public virtual IList<Comment> Replies { get; set; } public virtual string Description { get; set; } public virtual Article Article { get; set; } } 回答1: The

EF Core 2.0 TransactionScope Error

流过昼夜 提交于 2019-12-07 10:31:32
问题 I am trying to use TransactionScope in my SELECT query in EntityFramework Core 2.0. However I am getting this error : "Enlisting in Ambient transactions is not supported." The idea is to implement "NO LOCK" option (I know it's not a good idea to have that option in place but it's vendor's requirement) when I do select query. So I added an extension method (Entity Framework with NOLOCK) public static async Task<List<T>> ToListReadUncommittedAsync<T>(this IQueryable<T> query) { using (var scope

How to create CreatedOn and UpdatedOn using EF Core 2.1 and Pomelo

蹲街弑〆低调 提交于 2019-12-07 07:28:41
问题 In Code First approach, how to define my entity so that: CreatedOn NOT NULL - value is generated on insert by the db with the current timestamp Updated NULL - value is generated on update by the db with the current timestamp Sample Entity: public class MyEntity { public int Id { get; set; } public string Name { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(TypeName = "TIMESTAMP")] public DateTime CreatedOn { get; set; } [Column(TypeName = "TIMESTAMP")] public