无法将 lambda 表达式转换为类型 "DbContextOptions <DataContext>",因为它不是委托类型
Cannot convert lambda expression to type "DbContextOptions <DataContext>" because it is not a delegate type
我创建了 class DataContect,它继承自 class 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"));
}
public void SetCreateDatabase()
{
Console.WriteLine("Check for database availability: ");
using (var transaction = _db.Database.BeginTransaction())
{
try {
if(_db.Database != null)
{
Console.WriteLine("Done!\r\n");
}
else
{
_db.Database.EnsureCreated();
Console.WriteLine("Database was created!\r\n");
}
transaction.Commit();
}
catch (DbException error)
{
Console.WriteLine(error);
transaction.Rollback();
}
}
}
}
}
我的 Program.cs 文件:
using System;
using DbInitialize.Provider;
using ProjDAL.EF;
namespace DbInitialize
{
class Program
{
private static readonly DbInitializeProvider _db;
delegate void Display();
static Program()
{
_db = new DbInitializeProvider();
}
static void Main(string[] args)
{
try
{
Display display = _db.SetCreateDatabase;
display.Invoke();
Console.WriteLine($"\r\n{new string('-', 80)}");
Console.WriteLine("For continue press any button...");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
}
我收到错误:无法将 lambda 表达式转换为类型 "DbContextOptions " 因为它不是委托类型 如何正确创建 DataContext 实例并设置选项参数?
如果您需要更多信息,请告诉我。谢谢你的帮助。
创建 DataContext
class 时,参数与您在 DataContext
构造函数中定义的参数不匹配。它需要一个类型为 DbContextOptions<DataContext>
的对象,但您提供的操作带有选项参数 options => options.UseSqlServer("Data Source=.\SQLEXPRESS;Initial Catalog=ProjAppTest;Integrated Security=True;MultipleActiveResultSets=true")
您需要构建选项对象并将实例提供给构造函数:
var optionsBuilder = new DbContextOptionsBuilder<DataContext>();
optionsBuilder.UseSqlServer("YOUR CONNECTION STRING");
_db = new DataContext(optionsBuilder.Options)
或者您可以使用不带参数的构造函数,并在 DataContext
class 的 OnConfiguring
方法中配置它。
在此处查看文档:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
我创建了 class DataContect,它继承自 class 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"));
}
public void SetCreateDatabase()
{
Console.WriteLine("Check for database availability: ");
using (var transaction = _db.Database.BeginTransaction())
{
try {
if(_db.Database != null)
{
Console.WriteLine("Done!\r\n");
}
else
{
_db.Database.EnsureCreated();
Console.WriteLine("Database was created!\r\n");
}
transaction.Commit();
}
catch (DbException error)
{
Console.WriteLine(error);
transaction.Rollback();
}
}
}
}
}
我的 Program.cs 文件:
using System;
using DbInitialize.Provider;
using ProjDAL.EF;
namespace DbInitialize
{
class Program
{
private static readonly DbInitializeProvider _db;
delegate void Display();
static Program()
{
_db = new DbInitializeProvider();
}
static void Main(string[] args)
{
try
{
Display display = _db.SetCreateDatabase;
display.Invoke();
Console.WriteLine($"\r\n{new string('-', 80)}");
Console.WriteLine("For continue press any button...");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
}
我收到错误:无法将 lambda 表达式转换为类型 "DbContextOptions " 因为它不是委托类型 如何正确创建 DataContext 实例并设置选项参数?
如果您需要更多信息,请告诉我。谢谢你的帮助。
创建 DataContext
class 时,参数与您在 DataContext
构造函数中定义的参数不匹配。它需要一个类型为 DbContextOptions<DataContext>
的对象,但您提供的操作带有选项参数 options => options.UseSqlServer("Data Source=.\SQLEXPRESS;Initial Catalog=ProjAppTest;Integrated Security=True;MultipleActiveResultSets=true")
您需要构建选项对象并将实例提供给构造函数:
var optionsBuilder = new DbContextOptionsBuilder<DataContext>();
optionsBuilder.UseSqlServer("YOUR CONNECTION STRING");
_db = new DataContext(optionsBuilder.Options)
或者您可以使用不带参数的构造函数,并在 DataContext
class 的 OnConfiguring
方法中配置它。
在此处查看文档:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext