Tag: entity framework core

UseSqlServer方法缺少MVC 6

我正在尝试在MVC 6中实现Entity Framework 7,并在此页面上说它要做 services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration[“Data:DefaultConnection:ConnectionString”])); 但对我来说, UseSqlServer方法不可见? 有谁知道如何让它可见? 或者这是配置entity framework的旧方法? 我的startup.cs文件看起来像这样 using FluentValidation; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; namespace me.namespace.project { public class Startup { public static IConfiguration Configuration { get; set; } public Startup(IHostingEnvironment env) { // Setup configuration sources. Configuration = new Configuration() .AddJsonFile(“config.json”) .AddEnvironmentVariables(); } […]

没有数据库提供程序配置EF7

使用Entity Framework 7和MVC6时,我似乎收到此错误消息 System.InvalidOperationException未配置任何数据库提供程序。 在设置服务时,通过在DbContext类或AddDbContext方法中覆盖OnConfiguring来配置数据库提供程序。 我相信我已经做了我应该做的一切,所以也许它是一个错误。 我使用的是Entity Framework的7.0.0-beta7版本。 我已经设置了我的DbContext,一个接口,所以我可以模拟DbContext(在EntityFramework 6中需要进行unit testing)。 我的服务将接口作为构造函数,我在MVC 6中设置了DI。 在我的Startup.cs文件中,我有以下内容 public void ConfigureServices(IServiceCollection services) { // entity framework services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration[“Data:MyConnection:ConnectionString”]) ); // Add MVC services to the services container. services.AddMvc(); // new context on each request services.AddScoped(); } 我已经检查了我的connectionString,并且返回了一个有效的连接。 我还检查了我的服务,正在注入该对象,并且它不是null,因此应该全部工作。 我的config.json文件看起来像这样 { “Data”: { “MyConnection”: { “ConnectionString”: “Server=(local);Database=XXXX;Trusted_Connection=True;” […]

如何使用EF Core 2.1.0和代理进行延迟加载

我有以下型号: 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 Sessions { get; set; } public […]

尝试在.Net Standard项目中设置Entity Framework核心

我想知道我是否可以轻松地在.NET Standard 2.0项目中设置我的EntityFrameworkCore。 我正在学习本教程,但它需要.NET Core或Framework。 当我到达这一步时: Scaffold-DbContext “Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models 我收到了这个错误: 启动项目’projectName’的目标是框架’.NETStandard’。 没有与此框架关联的运行时,并且无法直接执行以其为目标的项目。 要将Entity Framework核心软件包管理器控制台工具与此项目一起使用,请添加一个针对引用此项目的.NET Framework或.NET Core的可执行项目,并将其设置为启动项目; 或者,更新此项目以跨目标.NET Framework或.NET Core。 我想知道我是否可以在.NET Standard中设置我的实体,或者是否有我应该做的最佳实践?

entity framework核心创建和更新字段

我的所有实体都有四个自定义字段。 这四个字段是CreatedBy , CreatedDate , UpdatedBy和UpdatedDate 。 有没有办法挂钩到Entity Framework核心事件,以便在插入时它将使用当前用户填充当前DateTime和CreatedBy的CreatedDate ? 当数据库有更新时,它会使用当前用户填充当前DateTime和UpdatedBy吗?

将Env Conn字符串注入.NET Core 2.0 w / EF Core DbContext中的不同类lib比启动prj并实现IDesignTimeDbContextFactory

老实说,我不敢相信这有多难……首先是我要求的要求: 实现entity frameworkCore IDesignTimeDbContextFactory , IDbContextFactory被重命名, 以免对开发人员造成什么困扰 我不想不止一次加载appsettings.json 。 一个原因是因为我的迁移在MyClassLibrary.Data域中运行,并且该类库中没有appsettings.js文件,我将不得不Copy to Output Directory appsettings.js 。 另一个原因是它不是很优雅。 所以这就是我目前的工作原理: using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; using AppContext = Tsl.Example.Data.AppContext; namespace Tsl.Example { public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory { public AppContext CreateDbContext(string[] args) { string basePath = AppDomain.CurrentDomain.BaseDirectory; string envName = Environment.GetEnvironmentVariable(“ASPNETCORE_ENVIRONMENT”); IConfigurationRoot configuration = new […]

如何根据XUnit测试隔离EF InMemory数据库

我正在尝试使用InMemory EF7数据库进行我的xunit存储库测试。 但我的问题是,当我尝试Dispose创建的上下文时,内存db仍然存在。 这意味着一个测试涉及其他。 我已经阅读了本文unit testingentity framework7与内存数据存储 ,我试图在我的TestClass的构造函数中设置上下文。 但这种方法不起作用。 当我单独运行测试时一切正常,但我的第一个测试方法是在DB中添加一些东西,第二个测试方法从之前的测试方法开始使用脏DB。 我尝试将IDispose添加到测试类中,但方法DatabaseContext和DB在内存中保留。 我错了什么我错过了什么? 我的代码看起来像: using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Xunit; namespace Fabric.Tests.Repositories { /// /// Test for TaskRepository /// public class TaskRepositoryTests:IDisposable { private readonly DatabaseContext contextMemory; /// /// Constructor /// public TaskRepositoryTests() { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseInMemoryDatabase(); […]

嵌套tphinheritance成员的ef-core load collection属性

给出以下类结构 public class Parent { public Guid Id { get; public List Children { get; set; } } public abstract class BaseChild { public int Id { get; set; } public string ChildName { get; set; } } public class NormalChild : BaseChild { public DateTime BirthDate { get; set; } } public class RichChild […]

Database First Scaffold-DbContext上的“构建失败”

我正在尝试从数据库生成类(EntityFramework的数据库第一种方法)。 为方便起见,我或多或少地跟随本教程: https : //docs.efproject.net/en/latest/platforms/full-dotnet/existing-db.html 我正处于Visual Studio包管理器控制台中运行相当于这行代码的地步: Scaffold-DbContext “Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -Verbose 这行代码生成错误(启用-Verbose模式): Using startup project ‘EFSandbox’. Using project ‘EntityFrameworkCore’ Build started… Build failed. 我没有看到任何产生任何有意义输出的选项,我看不到有关此特定错误的文档。 如果它有帮助,那么这个项目目前没有project.json文件。 一切都在.csproj文件中,我没有手动编辑。

entity framework核心使用多个DbContexts

我有一个问题,当我尝试访问我的PartsDbContext中的字段时,我收到以下错误: System.Data.SqlClient.SqlException:’无效的对象名’fieldName” 这似乎是因为我正在尝试使我的PartsDbContext使用与我的ApplicationDbContext相同的数据库,该数据库与Identity一起使用。 我需要知道如何设置第二个dbcontext以使用使用/创建不同数据库的EF核心。 我已经尝试创建第二个连接字符串,但这会让我犯这个错误: System.Data.SqlClient.SqlException:’无法打开登录请求的数据库“PartsDb”。 登录失败。 用户’DESKTOP-4VPU567 \ higle’登录失败。’ 这是我的代码: appsettings.json “ConnectionStrings”: { “DefaultConnection”: “Server=(localdb)\\mssqllocaldb;Database=aspnet-PrecisionCustomPC-b14db89e-86ad-4855-a17f-ac64a04339aa;Trusted_Connection=True;MultipleActiveResultSets=true”, “PartsConnection”: “Server=(localdb)\\mssqllocaldb;Database=PartsDb” }, “Logging”: { “IncludeScopes”: false, “LogLevel”: { “Default”: “Warning” } } PartsDbContext.cs public class PartsDbContext : DbContext { public DbSet Towers { get; set; } public DbSet Motherboards { get; set; } public PartsDbContext(DbContextOptions options) : base(options) […]