Tag: entity framework core

EF。 连接没有关闭。 连接的当前状态是连接

我有jwt auth: var messageHandlers = new JwtMessageHandler(_serviceProvider); app.UseJwtBearerAuthentication(new JwtBearerOptions { AutomaticAuthenticate = true, AutomaticChallenge = true, Events = new JwtBearerEvents { OnMessageReceived = messageHandlers.OnMessageReceived, }, TokenValidationParameters = tokenValidationParameters }); JwtMessageHandler是我的自定义处理程序。 在处理程序中,我必须对数据库进行一些查询,因此我传递ServiceProvider并解析我的用户服务: public class JwtMessageHandler { private IUserService _userService; public async Task OnMessageReceived(MessageReceivedContext arg) { //parsing header, get claims from token … _userService = (IUserService)arg.HttpContext.RequestServices.GetService(typeof(IUserService)); var […]

Ef core fluent api设置所有列类型的接口

不幸的是,ef核心不支持TPC模式,但我们需要这种行为。 我写了一个名为IBase的接口,每个实体都实现了这个接口: public interface IBase { Guid Id { get; set; } [Column(TypeName = “datetime2”)] DateTime CreateDate { get; set; } [Required] [StringLength(255)] string CreateUser { get; set; } bool Deleted { get; set; } } 我想摆脱Annotations以使用Fluent API配置。 但是我有大约20个不同的实体和7个Base-Values,我不想一遍又一遍地做出相同的配置: modelBuilder.Entity() .Property(e => e.CreateDate) .HasColumnType(“datetime2(2)”) .IsRequired(); 任何想法如何为实施IBase的所有实体配置一次Base-Property?

密码保护的SQLite与entity framework核心

我正在尝试创建一个受密码保护的SQLite数据库,以便在使用Entity Framework Core的WPF应用程序中使用它。 我找到了如何从现有的SQLite DB(数据库第一种方法)生成我的DbContext和实体,但我无法使用受密码保护的DB。 实际上,我甚至不确定如何创建受密码保护的SQLite DB。 加密数据库和受密码保护的数据库有什么区别?

无法加载文件或程序集Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.1.0.0

更新到新包Microsoft.EntityFrameworkCore.SqlServer 1.1.2后,我尝试创建DBContext时出错: System.IO.FileLoadException发生HResult = 0x80131040 消息=无法加载文件或程序集’Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.1.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60’或其依赖项之一。 定位的程序集的清单定义与程序集引用不匹配。 (来自HRESULT的exception:0x80131040)Source = Microsoft.EntityFrameworkCore StackTrace:位于C:\ src \ backend \中Services.Infrastructure.Data.SqlServerDbContext..ctor(DatabaseOptions databaseOptions)的Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions选项) Packages \ Services.Infrastructure \ Data \ SqlServerDbContext.cs:第16行,位于C:\ src \ backend \ Modules \ Translations \ Translations.Api \ Data \ TranslationsDbContext.cs中的Translations.Api.Data.TranslationsDbContext..ctor(DatabaseOptions databaseOptions) :第16行 我的基础DbContext public class SqlServerDbContext : DbContext { private readonly DatabaseOptions _databaseOptions; […]

左外连接entity framework核心

我正在尝试使用EF7(7.0.0-rc1-final),vNext RC1(rc1-final)和SQL Server 2014执行左外连接请求 数据库: 宠物:Id,名字 用户:Id,Name,#PetId 这个工作: var queryWorks = from u in _context.Users join p in _context.Pets on u.PetId equals p.Id into pp from p in pp.DefaultIfEmpty() select new { UserName = u.Name, Pet = p }; 但这个不起作用(Message =“Sequence contains no elements”): var queryFails = from u in _context.Users join p in _context.Pets […]

实体类型’Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin ‘需要定义一个键

我有一个使用EF7的ASP.NET5 MVC应用程序。 它到目前为止工作得很好,我能够在数据库中添加迁移和持久数据。 现在,在将Identity添加到我的数据层项目后,我在尝试添加新迁移时遇到此错误: 实体类型’Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin’需要定义一个键 我的上下文源自IdentityDbContext: public class ASSWebApiContext : IdentityDbContext AppUser类: using Microsoft.AspNet.Identity.EntityFramework; using System; namespace ASS.DomainDataModel.Models { public class AppUser : IdentityUser { public string AppUserId { get; set; } public DateTime FirstFlight { get; set; } } } project.json { “version”: “1.0.0-*”, “description”: “ASS.DomainDataModel Class Library”, “authors”: [ “xxxx” ], “tags”: [ […]

ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final issue – 编译器想要引用7.0.0.0,但未找到

我有相同的问题。 我在project.json文件中添加了以下依赖项: “dependencies”: { “EntityFramework”: “7.0.0-beta4”, “EntityFramework.Core”: “7.0.0-rc1-final”, “EntityFramework.SqlServer”: “7.0.0-beta8”, “EntityFramework.Commands”: “7.0.0-rc1-final” }, “commands”: { “web”: “Microsoft.AspNet.Server.Kestrel”, “ef”: “EntityFramework.Commands” }, … 我用dnu install EntityFramework和dnu install EntityFramework.SqlServer来安装软件包。 “EntityFramework”: “7.0.0-beta4”,由安装程序本身自动编写在dependencies节点下。 问题/问题1:令我惊讶的是,当我为EntityFramework提供智能时,我提供的可用版本仅为6.1.3 ! 问题2:当我使用dnu build编译我的应用程序时(我在dependencies节点下手动添加EntityFramework程序集( .Core和.Commands )后运行了dnu restore命令)我收到了一堆编译错误: The type ‘DbContextOptionsBuilder’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘EntityFramework.Core, […]

在Startup.cs中添加DbContextOptions而不注册数据存储

我的问题是下面的代码在启动期间没有注册数据存储。 这是我从应用程序的响应中得到的特定“错误”语句: An unhandled exception occurred while processing the request. InvalidOperationException: No data stores are configured. Configure a data store by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services. Microsoft.Data.Entity.Storage.DataStoreSelector.SelectDataStore(ServiceProviderSource providerSource) 在ConfigureServices(IServiceCollection服务)中,我试图在lambda中为我的DbContext指定DbContextOptions。 码: services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext( options => options.UseSqlServer(Configuration.Get(“Data:DefaultConnection:ConnectionString”)) ); 在我的DbContext中,我有一个构造函数,它将选项发送到base,代码: public MyContext(DbContextOptions options) : base(options) { } […]

.NET Core 2.1 Identity为所有用户提供相关角色

我正在尝试为用户管理管理页面提取所有Identity用户及其相关角色。 我认为这将相当容易,但显然不是。 我尝试过以下解决方案: https : //stackoverflow.com/a/43562544/5392786但到目前为止还没有解决。 这是我到目前为止: ApplicationUser: public class ApplicationUser : IdentityUser { public List<IdentityUserRole> Roles { get; set; } } 的DbContext public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } } 启动标识代码 services.AddIdentity(options => options.Stores.MaxLengthForKeys = 128) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); 我要显示列表的Razor页面: public class IndexModel : PageModel { private readonly UserManager […]

entity framework核心 – 延迟加载

鞠躬我的Visual Studios请求,我使用Entity Framework Core(1.0.1)开始了我的最新项目 所以编写我的数据库模型,因为我总是使用’​​虚拟’说明符来启用List的延迟加载。 虽然在加载父表时,似乎子目录永远不会加载。 父模型 public class Events { [Key] public int EventID { get; set; } public string EventName { get; set; } public virtual List EventInclusions { get; set; } } 儿童模特 public class EventInclusions { [Key] public int EventIncSubID { get; set; } public string InclusionName { get; set; } […]