How to make a Connection between EF6 and asp.net core 3

-1

Goal:
Use EF 6 inside of ASP.net core 3 by using a project named ef.
Project EF is the main connection to the database sql server.

Problem:
I get an error saying

Error = Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057.

What part am I missing from the code in order retrieve the data in asp.net core 3.

public IActionResult Index()
{
    var ddf = _context.Blogs;


    return View();
}

Info:
*Uploaded the project at github (https://github.com/candyboyyy/WebApplication1_asp.netcore3)

enter image description here

CREATE TABLE [dbo].[Blogs]
(
    [BlogId] [INT] IDENTITY(1,1) NOT NULL,
    [Name] [NVARCHAR](200) NULL,
    [Url] [NVARCHAR](200) NULL,

    CONSTRAINT [PK_dbo.Blogs] 
        PRIMARY KEY CLUSTERED ([BlogId] ASC)
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                          ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

enter image description here

c#
entity-framework-6
asp.net-core-3.0
asked on Stack Overflow Nov 3, 2019 by What'sUP • edited Nov 4, 2019 by What'sUP

1 Answer

0

I would suggest try to establish a database connection first from your web project to your database. In your Startup.cs class, add this line of code var connectionString = Configuration.GetConnectionString("DefaultConnection"); under ConfigureServices.cs class. Then add services.AddDbContext to configure your EF and add services to the container.

Code snippet below.

using Microsoft.Extensions.DependencyInjection;

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();

                var connectionString = Configuration.GetConnectionString("DefaultConnection");

    services.AddDbContext<BloggingContext>(options =>
                    options.UseSqlServer(connectionString), ServiceLifetime.Transient);

            }

Hope that helps.

answered on Stack Overflow Nov 4, 2019 by Juniuz • edited Nov 4, 2019 by Juniuz

User contributions licensed under CC BY-SA 3.0