entity-framework-core

Combine BinaryExpression and Expression<Func<dynamic, bool>> in C#

前提是你 提交于 2021-02-16 13:49:08
问题 How can I combine BinaryExpression and Expression<Func<dynamic / T, bool>> ? For example: void AddGlobalFilter<T>(Expression<Func<T, bool>> expr) { var parameter = Expression.Parameter(type, "t"); var member = Expression.Property(filter.Parameter, field); var constant = Expression.Constant(null); var body = Expression.Equal(member, constant); var combine = Expression.AndAlso(body, expr); } I am trying to define global filter for Entity Framework (EF) Core. The problem is I must manually

EF Core one-to-zero relationship one way

£可爱£侵袭症+ 提交于 2021-02-16 06:11:02
问题 Can a relationship one-to-one be created only one way? public class Class1 { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Class1Id { get; set; } ... } public class Class2 { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Class2Id { get; set; } public int? RelationshipId { get; set; } public virtual Class1 Relationship { get; set; } ... } And the configuration looks like this public void Configure(EntityTypeBuilder<Class2> builder) { builder

EF Core one-to-zero relationship one way

别等时光非礼了梦想. 提交于 2021-02-16 06:08:06
问题 Can a relationship one-to-one be created only one way? public class Class1 { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Class1Id { get; set; } ... } public class Class2 { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Class2Id { get; set; } public int? RelationshipId { get; set; } public virtual Class1 Relationship { get; set; } ... } And the configuration looks like this public void Configure(EntityTypeBuilder<Class2> builder) { builder

Does EF Core 3.1 support DB First approach?

混江龙づ霸主 提交于 2021-02-16 04:01:10
问题 We are porting an ASP.NET MVC 4.x application to ASP.NET Core 3.1 . The current application is using EF 6.x DB first approach. As a part of this migration we are going to use EF Core 3.1 as an alternative to the current EF 6.x . So the question is: Does EF Core 3.1 support DB First approach? If not, what are the options? Are we left with only code first approach? Appreciate your helps. 回答1: Yes. It supports DB First Approach since .NET Core 1.0 until now. You need to download 4 from nugets

EF Core with Cosmos DB provider, UserManager AddLoginAsync gives ConcurrencyFailure

亡梦爱人 提交于 2021-02-15 07:35:24
问题 Creating a new user, fetching the user again with usermanager for testing, and then using the method AddLoginAsync with the recently fetched user gives the error ConcurrencyFailure, Optimistic concurrency failure, object has been modified. When fetching the user the "ConcurrencyStamp" has the correct etag, but after the "AddLoginAsync" I can see the user object has an invalid etag, the ConcurrencyStamp is a GUID. I have followed the documentation and added this to the IdentityUser model

How to set multiple services from Entity Framework Core on Repository Pattern?

孤街浪徒 提交于 2021-02-15 07:34:59
问题 I am trying to build a structure using repository pattern in entity framework core. I have an generic interface and a service for general operations. They simply do CRUD transactions. My interface: public interface IGeneralService<TEntity> where TEntity : class { void Delete(TEntity entityToDelete); void Delete(object id); IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = ""

How to set multiple services from Entity Framework Core on Repository Pattern?

半世苍凉 提交于 2021-02-15 07:34:16
问题 I am trying to build a structure using repository pattern in entity framework core. I have an generic interface and a service for general operations. They simply do CRUD transactions. My interface: public interface IGeneralService<TEntity> where TEntity : class { void Delete(TEntity entityToDelete); void Delete(object id); IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = ""

The LINQ expression 'OUTER APPLY Projection Mapping in EF .NET Core 5 Exception

拜拜、爱过 提交于 2021-02-11 17:35:42
问题 After migrating from .NET Core 2.x to .NET Core 5.0, we are facing this problem. Error: (Added as a CODE for better readability) The LINQ expression 'OUTER APPLY Projection Mapping: ( SELECT e0.Id, e0.FirstName, e0.MiddleName, e0.LastName FROM Employees AS e0 WHERE (((e0.Status != 4) && EXISTS ( Projection Mapping: SELECT 1 FROM FunctionRoles AS f0 WHERE t.Id == f0.SchoolId)) && (e0.FunctionRoleId == (Projection Mapping: EmptyProjectionMember -> 0 SELECT TOP(1) f1.Id FROM FunctionRoles AS f1

How can I load all child records recursively?

折月煮酒 提交于 2021-02-11 14:24:58
问题 I have this Project class: public class Project { public int Id { get; set; } public int? ParentId { get; set; } public List<Project> ChildProjects { get; set; } // more properties } This is my attempt to load all descendants of any given project: private async Task<List<Project>> LoadDescendantsOf(Project project) { project.ChildProjects = await db.Projects .Where(p => p.ParentId == project.Id) .ToListAsync(); foreach (Project childProject in project.ChildProjects) { yield return

Migrating One-to-Many to Many-to-Many with the new EF Core 5.0

谁说我不能喝 提交于 2021-02-11 14:22:38
问题 I have an ASP.NET Core 5 project (migrated from ASP.NET Core 3.1). The project uses EF Core. EF Core 5 has built-in support for Many-to-Many. The main reason I first migrated from 3.1 to 5.0. Now my project has a One-to-Many relationship, which I want to convert to a Many-to-Many. Is such a relationship migration possible? Current code: public class File { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] public int Id { get; set; } [DatabaseGenerated(DatabaseGeneratedOption