我创建了类DataContect,它是从类IdentityDbContext继承而来的:using ProjDAL.Entities;using ProjDAL.Relations;using ProjDAL.Services;using Microsoft.AspNetCore.Identity.EntityFrameworkCore;using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Design;using Microsoft.Extensions.Configuration;namespace ProjDAL.EF{ public class DataContext : IdentityDbContext<ApplicationUser> { public DataContext(DbContextOptions<DataContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); }.......................................................}解决方案具有控制台应用程序,我在其中创建新的DataContext:using System;using DbInitialize.Interface;using ProjDAL.EF;namespace DbInitialize.Provider { public class DbInitializeProvider : IDbInitialize { private DataContext _db; public DbInitializeProvider() { _db = new DataContext(options => options.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=ProjAppTest;Integrated Security=True;MultipleActiveResultSets=true"));我收到错误:无法将lambda表达式转换为类型“DbContextOptions”,因为它不是委托类型 如何正确创建DataContext实例并设置选项参数?如果您需要更多信息,请告诉我。感谢您的帮助。
1 回答

UYOU
TA贡献1878条经验 获得超4个赞
创建类时,参数与构造函数中定义的内容不匹配。它需要一个类型的对象,但是您正在提供带有选项参数的操作DataContextDataContextDbContextOptions<DataContext>options => options.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=ProjAppTest;Integrated Security=True;MultipleActiveResultSets=true")
您需要构建 options 对象并将实例提供给构造函数:
var optionsBuilder = new DbContextOptionsBuilder<DataContext>();
optionsBuilder.UseSqlServer("YOUR CONNECTION STRING");
_db = new DataContext(optionsBuilder.Options)
或者,可以使用不带参数的构造函数,并在方法的类中对其进行配置。DataContextOnConfiguring
请参阅此处的文档:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
- 1 回答
- 0 关注
- 146 浏览
添加回答
举报
0/150
提交
取消