entity-framework-core

The term 'Add-migration' is not recognized - VS2017 and EntityFrameworkCore

a 夏天 提交于 2019-11-30 08:03:13
I am posting this question just in case the solution I found would help someone else out. While working in Visual Studio 2017 rc4 each time I tried to run the Add-Migration command in the Package Manager console I get the following error: The term 'Add-migration' is not recognized... You just need to initialize the powershell module. I believe this is a bug, but it's pretty easy to work around. All you need to do is find the init.ps1 file and dot source it. Just type this into the package manager console: C:\Users\YourUserAccount\.nuget\packages\Microsoft.EntityFrameworkCore.Tools\1.1.0

AddDbContext or AddDbContextPool

感情迁移 提交于 2019-11-30 08:02:06
For Asp.net Core apps, which one do we have to use? AddDbContext or AddDbContextPool ? According to EF Core documentation, AddDbContextPool provides high performance but the default Asp.net Core project templates use AddDbContext . The answer is here (under "DbContext pooling"): https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#dbcontext-pooling DbContext is not thread-safe. So you cannot reuse the same DbContext object for multiple queries at the same time (weird things happen). The usual solution for this has been to just create a new DbContext object each time you need one.

Translating generic eager load method from EF6 to EF Core

£可爱£侵袭症+ 提交于 2019-11-30 07:33:00
问题 For EF6, I had a method in my generic repository that I exposed to all service layers in order to retrieve entities from the database with any nested properties as needed: public IQueryable<T> OldMethod(params Expression<Func<T, object>>[] includeProperties) { var queryable = set.AsQueryable(); return includeProperties.Aggregate(queryable, (current, includeProperty) => current.Include(includeProperty)); } This way, I could use the method in the following way: var data = repo.OldMethod(x => x

EntityFramework Commands in ASP.Net 5 Class Library Package?

元气小坏坏 提交于 2019-11-30 07:02:55
I am trying to develop my first ASP.Net web application and in my solution I have two projects. A Web Application and Class Library (Package) . When I build the ASP.Net 4.5 version of this application I put my Entity Framework 6 entities into a class library, so I am doing that in the ASP.Net 5 version. The problem is when I install EntityFramework.Commands to the class library I get the error: The dependency EntityFramework.Command 7.0.0-rc1-final in Project DBEntities does not support framework .NetPlatform, Version = 5.4 My understanding from this SO question is that dotnet5.4 is the new

Entity Framework Core 1.0 unit of work with Asp.Net Core middleware or Mvc filter

て烟熏妆下的殇ゞ 提交于 2019-11-30 06:46:42
问题 I am using EF Core 1.0 (previously known ad EF7) and ASP.NET Core 1.0 (previously known as ASP.NET 5) for a RESTful API. I'd like to have some unit of work scoped to an http request in such a way that when responding to the HTTP request either ALL the changes made to the DbContext will be saved onto the database, or none will be saved (if there was some exception, for example). In the past I have used WebAPI2 for this purpose with NHibernate by using an Action filter where I begin the

Trying to set-up Entity Framework core in .Net Standard project

一曲冷凌霜 提交于 2019-11-30 06:40:50
问题 I was wondering if I could set-up my EntityFrameworkCore in a .NET Standard 2.0 project easily. I was following this Tutorial but it requires either .NET Core or Framework. When I got to this step: Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models I got this error: Startup project 'projectName' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects

EF Core Error - No project was found. Change the current working directory or use the --project option

馋奶兔 提交于 2019-11-30 06:22:49
I am using Visual Studio 2015 and dotnet core and trying to develop an EF Core Code First project using Sqlite and this documentation / tutorial, which also uses Sqlite => NET Core - New Database When I try to add an initial migration from the command line ( I am CD-ed into the folder that my data model project is located in) by issuing the following command dotnet ef migrations add InitialMigration ...I get the following Error. No project was found. Change the current working directory or use the --project option. I even tried using the --project option like so. > dotnet --project "C:\Shiva

Entity Framework Core Creation and Update fields

此生再无相见时 提交于 2019-11-30 05:20:58
I have four custom fields on all of my entities. The four fields are CreatedBy , CreatedDate , UpdatedBy and UpdatedDate . Is there a way to hook into Entity Framework core events so that on an insert it will populate the CreatedDate with the current DateTime and CreatedBy with the current user? When there is an update to the database it would populate UpdatedDate with the current DateTime and UpdatedBy with the current user? Tseng Basically @Steve's approach is the way to go, but the current implementation of it makes it hard to unit test your project. With a little bit of refactoring, you

“Build failed” on Database First Scaffold-DbContext

谁说我不能喝 提交于 2019-11-30 04:10:48
I'm trying to generate classes from a database (EntityFramework's database first approach). For convenience, I'm more or less walking along with this tutorial: https://docs.efproject.net/en/latest/platforms/full-dotnet/existing-db.html I'm at a point where I am running the equivalent of this line of code in the Visual Studio Package Manager Console: Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Verbose This line of code is generating the error (with -Verbose mode on): Using startup project 'EFSandbox'.

How to create a Clustered Index with Entity Framework Core

拜拜、爱过 提交于 2019-11-30 03:32:25
问题 From EF6.1, we have a way of specifying a clustered index on a property public class Person { [Index(IsClustered = true, IsUnique = true)] public long UserName { get; set; } } But this Index attribute does not seem to be in EF Core right now? In EF Core how do you achieve this? 回答1: From the current EF Core documentation - Indexes section: Data Annotations Indexes can not be created using data annotations. But for sure you can specify that via Fluent API (note the extension methods having