entity-framework-core

How to update naviation property with new values in EF Core?

ε祈祈猫儿з 提交于 2020-02-06 19:02:31
问题 Soo... I have this simple model for Product : public class Product { public int Id { get; set; } // Other things public ICollection<ProductAttribute> ProductAttributes { get; set; } } And ProductAttributes with ProductId and Name as multifield keys. public class ProductAttribute { public int? ProductId { get; set; } // Key public ProductAttributeName? Name { get; set; } // Key public string Value { get; set; } } And in my WebAPI project I have this method: public async Task<IActionResult>

EF Core: Eager loading (.Include) sub-categories (self-reference)

本秂侑毒 提交于 2020-02-06 10:44:34
问题 We have something like this var categories = _context.Categories.Include("Categories1.Categories1.Categories1"); That works and handles sub-categories up to 4-level deep (which is enough for now but who knows the future) Is there a better way to do it? More info We use database-first. Category table has these columns: Id ParentCategoryId <-- this has foreign key to Category.Id 回答1: In this particular case I think a recursive property might be a good bet. Trying to do this from memory (been

EF Core: Eager loading (.Include) sub-categories (self-reference)

混江龙づ霸主 提交于 2020-02-06 10:41:29
问题 We have something like this var categories = _context.Categories.Include("Categories1.Categories1.Categories1"); That works and handles sub-categories up to 4-level deep (which is enough for now but who knows the future) Is there a better way to do it? More info We use database-first. Category table has these columns: Id ParentCategoryId <-- this has foreign key to Category.Id 回答1: In this particular case I think a recursive property might be a good bet. Trying to do this from memory (been

Linq on EF7 doesnt work with Joins and Dates?

流过昼夜 提交于 2020-02-06 04:57:08
问题 I am having some troubles running a query from a Controller on my ASP.NET MVC6 EF7 web app... The Model and DbContext are in this previous ask: EF7 Incorrect configuration of the DBContext? The problem appears when I try to run the following Linq query which contains a couple of joins and tries to get some entries from a Database for a particular date... public IActionResult GetEntries(int year, int month, int day) { //_context.Database.SetCommandTimeout(180); string dateTest = new DateTime

Add two expressions to create a predicate in Entity Framework Core 3 does not work

情到浓时终转凉″ 提交于 2020-02-04 07:09:21
问题 I am trying to build an "And" predicate method using C# with Entity Framework Core 3 in a .NET Core application. The function adds two expressions to each other and passes it to an IQueryable code: public Expression<Func<T, bool>> AndExpression<T> (Expression<Func<T, bool>> left, Expression<Func<T, bool>> right) { var andExpression = Expression.AndAlso( left.Body, Expression.Invoke(right, left.Parameters.Single())); return Expression.Lambda<Func<T, bool>>(andExpression, left.Parameters); }

Add two expressions to create a predicate in Entity Framework Core 3 does not work

*爱你&永不变心* 提交于 2020-02-04 07:02:28
问题 I am trying to build an "And" predicate method using C# with Entity Framework Core 3 in a .NET Core application. The function adds two expressions to each other and passes it to an IQueryable code: public Expression<Func<T, bool>> AndExpression<T> (Expression<Func<T, bool>> left, Expression<Func<T, bool>> right) { var andExpression = Expression.AndAlso( left.Body, Expression.Invoke(right, left.Parameters.Single())); return Expression.Lambda<Func<T, bool>>(andExpression, left.Parameters); }

Add two expressions to create a predicate in Entity Framework Core 3 does not work

会有一股神秘感。 提交于 2020-02-04 07:01:48
问题 I am trying to build an "And" predicate method using C# with Entity Framework Core 3 in a .NET Core application. The function adds two expressions to each other and passes it to an IQueryable code: public Expression<Func<T, bool>> AndExpression<T> (Expression<Func<T, bool>> left, Expression<Func<T, bool>> right) { var andExpression = Expression.AndAlso( left.Body, Expression.Invoke(right, left.Parameters.Single())); return Expression.Lambda<Func<T, bool>>(andExpression, left.Parameters); }

Change SQL Server database alphabetic column ordering in Entity Framework code-first to sequential

浪子不回头ぞ 提交于 2020-02-03 09:31:07
问题 I need to turn off alphabetic ordering in code first. Here is my class simplified public class Person { [Key,Column("PersonId")] public int Id { get; set; } [MaxLength(50)] public string PersonName{ get; set; } public DateTime? JoinDate{ get; set; } public int? Gender{ get; set; } } and when I run the commands to generate the database dnx ef migrations add InitialMigration dnx ef database update The database columns apart from the primary key generates in alphabetic order when I view it in

Translating query with GROUP BY and COUNT to Linq

岁酱吖の 提交于 2020-02-03 08:53:51
问题 I have a query to see how many entities Users have inserted ( Version = 1 ) and entities they've updated ( Version > 1 ). It queries the entire table and groups by the UserName of the record. This is the raw SQL query: SELECT [s.InternalUser].[UserName], COUNT(CASE WHEN s.Version = 1 THEN 1 END) AS [InsertCount], COUNT(CASE WHEN s.Version > 1 THEN 1 END) AS [UpdateCount] FROM [Sale] AS [s] INNER JOIN [InternalUser] AS [s.InternalUser] ON [s].[InternalUserId] = [s.InternalUser].[InternalUserId

OnModelCreating undefined in Entity Framework 7

Deadly 提交于 2020-02-03 03:13:17
问题 I am trying to setup my models' relations in EF7, but I faced the problem: OnModelCreating method and DbModelBuilder are undefined. At past, I used EF6, but now I try to migrate to EF7. Here is my code protected override void OnModelCreating(DbModelBuilder modelBuilder) { //Section -> many Category modelBuilder.Entity<Section>() .HasMany<Category>(p => p.Categories) .WithRequired(p => p.Section); //Section -> many PriceCategory modelBuilder.Entity<Section>() .HasMany<PriceCategory>(p => p