无法将lambda表达式转换为类型“ DbContextOptions <datacontext>”,因为它不是委托类型

时间:2019-02-03 11:44:16

标签: c# .net-core asp.net-identity entity-framework-core

我创建的类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"));
        }

        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实例并设置选项参数?

如果您需要更多信息,请告诉我。谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在创建 DataContext 类时,该参数与您在 DataContext 构造函数中定义的参数不匹配。它需要一个类型为 DbContextOptions 的对象,但是您正在提供带有选项参数 options => options的操作。UseSqlServer(“ Data Source =。\\ SQLEXPRESS; Initial Catalog = ProjAppTest ; Integrated Security = True; MultipleActiveResultSets = true“)

您需要构建options对象并将实例提供给构造函数:

  var optionsBuilder = new DbContextOptionsBuilder ();
optionsBuilder.UseSqlServer(“您的连接字符串”);

_db =新的DataContext(optionsBuilder.Options)
 

或者,您也可以使用不带参数的构造函数,并在 OnConfiguring 方法的 DataContext 类中对其进行配置。

请参阅此处的文档: https:// docs。 microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext