ef-core-2.0

EF Core Migrations manual edits possible?

∥☆過路亽.° 提交于 2019-12-13 16:51:01
问题 I am using EF Core 2.0 in my sample project with some value object configurations. I modify the code and generate migrations via CLI command line. In the last migration rather than adding a new database table as it should, it is trying to rename existing tables to each other and create an extra table for existing one. I could not figure out the reason for it. Issue is, since with EF Core the snapshot is a separate auto-generated file from the migration itself I don't want to modify the

Error when using FromSql and include (“The Include operation is not supported when calling a stored procedure.”)

旧时模样 提交于 2019-12-13 03:32:37
问题 I used Asp.Net Core version 2 and code-first. I tried to use .FromSql to call a stored procedure. I did the same as Microsoft said: var blogs = context.Blogs .FromSql($"SELECT * FROM dbo.SearchBlogs({searchTerm})") .Include(b => b.Posts) .ToList(); and my stored procedure only contains the line of code ALTER PROCEDURE [dbo].[GetCouffierSearch] AS BEGIN SET NOCOUNT ON; SELECT * FROM AspNetUsers END My code in the API: public IQueryable<ApplicationUser> SelectNearByUser() { var query = "execute

EF Core Include multiple inherited types TPH

依然范特西╮ 提交于 2019-12-13 00:14:39
问题 The problem Query BaseClass and Include information from derived types. For example: Base class Offer has two types one with a Card only ( CardOffer ) and with Multiple Cards ( CarouselOffer ). Question: How to query the BaseClass and Include the information from the derived types CardOffer and CarouselOffer? In the manner shown below: _dbContext.Offers.Include(...).ToList(); Currently I solved this by querying each inherited type var cards = await _dbContext.CardOffers .Include(c => c.Card)

Has column type of complex type in ef core 2.0

做~自己de王妃 提交于 2019-12-12 18:34:52
问题 I want to change column type of property using fluent api, but i have an error The expression 'x => x.NestedProp.Prop1' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'. please, i dont want to use DataAnnotations Here is my code (Classes): public class Class1 { public int Id { get; set; } public NestedProp NestedProp { get; set; } = new NestedProp(); } public class NestedProp { public decimal Prop1 { get; set; } } And OnModelCreating:

EF Core DbUpdateConcurrencyException does not work as expected

纵饮孤独 提交于 2019-12-12 15:35:39
问题 I have the following code that I am trying to update ClientAccount using ef core but they Concurrency Check fails: public class ClientAccount { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } [Required] [ConcurrencyCheck] public double Balance { get; set; } [Required] public DateTime DateTimeCreated { get; set; } [Required] public DateTime DateTimeUpdated { get; set; } } public class ClientRepository { private readonly MyContext context; public

Implement OrganizationUnit filter in ASP.NET Core

旧街凉风 提交于 2019-12-12 09:23:35
问题 In ASP.NET Boilerplate, there are built-in Data Filters like Tenant filter, and we could use SetTenantId to enable querying specific tenantId . As per the official document, it did not support custom Filter in EF Core, unlike EF 6.x. I am wondering how to achieve a similar filter for OrganizationUnitId parameter. This is what I have done so far: Override CreateFilterExpression : protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>() { Expression<Func<TEntity,

EFCore Update using UnitOfWork Repository and Service with AutoMapper

五迷三道 提交于 2019-12-11 08:54:53
问题 Following the N-tier architecture, I'm currently having issues while trying to update an entity. In my Controller, if I set the properties manually, the update works perfectly, if I set the properties mapping the ViewModel into the entity it generate the exception "...cannot track multiple objects with the same key". How can I solve this? This is my UnitOfWork: public class UnitOfWork : IUnitOfWork { private readonly CoreContext _context; private IGenericRepository<Currency>

How to get appsettings from project root to IDesignTimeDbContextFactory implementation in ASP.NET Core 2.0 and EF Core 2.0

删除回忆录丶 提交于 2019-12-11 08:07:25
问题 I am building an application in ASP.NET Core 2.0 and I am having problems with EntityFramework Migrations. I have my DbContext in a separate project ( SolutionName \ ProjectNamePrefix .Data) and therefore I created an implementation for the IDesignTimeDbContextFactory interface. I wanted to use different connection strings for different environments and I need appsettings.json for that. So after a quick search I found that I can create a new IConfigurationRoot object inside the

EF Core 2.1 Props inside where cause null reference

删除回忆录丶 提交于 2019-12-11 07:33:16
问题 When I migrated from EF 1.1 to 2.1 I started getting null reference error when iterating inside where condition, it seems like StatusCashAdvanceList is not loaded at the time where is executed. CashAdvance.cs: public virtual ICollection<StatusCashAdvance> StatusCashAdvanceList { get; set; } Stack Trace: NullReferenceException: Object reference not set to an instance of an object. lambda_method(Closure , CashAdvance) System.Linq.Enumerable+WhereSelectEnumerableIterator<TSource, TResult>

How to use custom name convention with owned types?

非 Y 不嫁゛ 提交于 2019-12-11 06:49:38
问题 public class ProcessInitialization { public Guid CorrelationId { get; set; } public MyProcessContext ProcessContext { get; set; } } public class MyProcessContext { public int? ProcessId { get; set; } } Without using my convention I got SELECT `process`.`CorrelationId` FROM `ProcessInitialization` AS `process` WHERE `process`.`ProcessContext_ProcessId` = 8 My convention makes snake_case from PascalCase : public static void SetSimpleUnderscoreTableNameConvention(this ModelBuilder modelBuilder,