Cannot convert lambda expression to type "DbContextOptions " because it is not a delegate type

.net-core asp.net-identity c# entity-framework-core

Question

I created class DataContect wich is inherited from 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);
        }
.......................................................
}

Solution has console application where I creating new 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();
                }
            }
        }   
    }
}

My Program.cs file:

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();
        }
    }
}

I get error: Cannot convert lambda expression to type "DbContextOptions " because it is not a delegate type How correct create DataContext instance and set options params?

If you need more information, please tell me. Thank you for help.

1
0
2/3/2019 11:44:15 AM

Accepted Answer

When creating your DataContext class the parameter does not match what you defined in the DataContext constructor. It expects an object of type DbContextOptions<DataContext> But you are providing an action with an options parameter options => options.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=ProjAppTest;Integrated Security=True;MultipleActiveResultSets=true")

You need to build the options object and provide the instance to the constructor:

var optionsBuilder = new DbContextOptionsBuilder<DataContext>();
optionsBuilder.UseSqlServer("YOUR CONNECTION STRING");

_db = new DataContext(optionsBuilder.Options)

Or alternatively you can use the constructor without arguments and configure it in your DataContext class in the OnConfiguring method.

See the documentation here: https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

1
2/3/2019 12:11:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow