entity-framework-core

Injecting Env Conn String into .NET Core 2.0 w/EF Core DbContext in different class lib than Startup prj & implementing IDesignTimeDbContextFactory

浪子不回头ぞ 提交于 2019-11-30 14:38:04
问题 I honestly cannot believe how hard this is...first off the requirements that I am going for: Implementing Entity Framework Core 2.0' IDesignTimeDbContextFactory which is IDbContextFactory renamed to be less confusing to developers as to what it does I do not want to have to do loading of appsettings.json more than once. One reason is because my migrations are running in the domain of MyClassLibrary.Data and there is no appsettings.js file in that class library, I would have to to Copy to

Configuring Dbcontext as Transient

大城市里の小女人 提交于 2019-11-30 14:37:22
问题 In ASP.NET Core / EntityFramework Core, the services.AddDbContext<> method will add the specified context as a scoped service. It's my understanding that that is the suggested lifetime management for the dbcontext by Microsoft. However, there is much debate in our engineer department over this and many feel that the context needs to be disposed of ASAP. So, what is the best way to configure the dbcontext as Transient that still maintains the same Repository pattern typically used (i.e.

How can I use database Views in a scaffolded DbContext with Entity Framework Core 1.0 (EF7)

白昼怎懂夜的黑 提交于 2019-11-30 14:18:39
Unfortunately Entity Framework Core 1.0 (formerly Entity Framework 7) does not yet support Views, and I'm trying to 'fake' it using a table. However the scaffolding dotnet dbcontext ef scaffold command doesn't currently recognize or generate views, and I want a single DbContext which allows querying a view and updating of tables. Is there a way to do this? This is the command I use to scaffold the DbContext: dotnet ef dbcontext scaffold -c MyStoreContext -o Model "Data Source=(local);Initial Catalog=DBNAME;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer --force (This puts all

How to make lazy-loading work with EF Core 2.1.0 and proxies

99封情书 提交于 2019-11-30 13:51:04
问题 I have the following models: public class Session { public int SessionID { get; set; } public int UserID { get; set; } public virtual User User { get; set; } } public class User { public int UserID { get; set; } public int OrganizationID { get; set; } public virtual ICollection<Session> Sessions { get; set; } public virtual Organization Organization { get; set; } } public class Organization { public int OrganizationID { get; set; } public virtual ICollection<User> Users { get; set; } } that

EF7 RC1 : Disable Cascade Delete

流过昼夜 提交于 2019-11-30 13:41:24
问题 In the RC1 of EntityFramework 7, released yesterday, Cascade Delete was added. To disable it per relationship, I can use : builder.Entity<Site>().HasOne(e => e.Person) .WithMany(x => x.Sites).Metadata.DeleteBehavior = DeleteBehavior.Restrict; I want to disable it globally for a DbContext, but I didn't find a way. How can I do ? 回答1: Someone stated on the github project forum that the only way to do it right now is to iterate through all relationships in the method OnModelCreating(ModelBuilder

Reset Entity Framework 7 migrations

时光毁灭记忆、已成空白 提交于 2019-11-30 13:27:23
I'm using Entity Framework 7 beta 5. I started with the mvc template with user management. I have added several migrations, during my trials. Now I want to delete my database, create a new initial migration and produce a new clean db with it. However the dnx ef commands do not have a command that I know of that will facilitate this. How do I proceed? What exactly do you need support from EF for? Here's what I'd do: Delete your database Delete all your migrations Configure the context to point to your new, clean database Run dotnet ef migrations add <initial-migration-name> 来源: https:/

dotnet ef not found in .NET Core 3

扶醉桌前 提交于 2019-11-30 12:37:16
问题 I'm following the docs in order to create an initial migration. When I execute dotnet , I get the help section, meaning that the PATH works properly. Then I try to execute the command below from the docs in console window: dotnet ef migrations add InitialCreate I get the following error: Could not execute because the specified command or file was not found. Possible reasons for this include: * You misspelled a built-in dotnet command. * You intended to execute a .NET Core program, but dotnet

EF Core Add Migration Debugging

眉间皱痕 提交于 2019-11-30 11:53:53
How can I step into OnModelCreating with a breakpoint and see if my logic is wrong or if the ModelBuilder is doing something I'm not expecting? I've seen lots of posts on how to debug the actual migration, but nothing on how to watch how the model code is being generated. I'm trying to implement some custom attributes on some of my entities, and it's being ignored; I'd like to see what my configuration is doing as it's generating the model code. You should be able to call Debugger.Launch() in your code. The just-in-time debugger should prompt you to attach a debugger when it hits that line. 来源

The current runtime target framework is not compatible with project

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 11:51:41
I am attempting to perform an initial migration after adding Entity Framework 7 to my Asp.Net 5 project. I am following this documentation . In my project.json I have specified: "commands": { "web": "Microsoft.AspNet.Server.Kestrel", "ef": "EntityFramework.Commands" }, "frameworks": { "dnxcore50": { } } I am attempting to execute the following command in the project directory via a command prompt: dnx ef migrations add MyFirstMigration But I get the following error: System.InvalidOperationException: The current runtime target framework is not compatible with ProjectName. The current runtime

EF7: The term 'add-migration' is not recognized as the name of a cmdlet

爷,独闯天下 提交于 2019-11-30 11:44:39
I have framework version set to: dnx46 in project.json. Also have the following packages: "dependencies": { "EntityFramework.Commands": "7.0.0-rc1-final", "EntityFramework.Core": "7.0.0-rc1-final", "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final" } However when I got into running enable-migrations command I get the following: The term 'enable-migrations' is not recognized as the name of a cmdlet Does anyone know how I get EF migrations running in latest .NET? Elijah Lofgren This is what worked for me to solve this issue: