dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

No DbContext was found in assembly #10287

Closed IsaaasI closed 2 years ago

IsaaasI commented 7 years ago

Dears, I am using VS2017 .NET Core 2.0 and I want to use entity framework MYSQL Database but I am facing issues on add-Migration

No DbContext was found in assembly 'ProjectName'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

Microsoft.AspNetCore.All 2.0.0-preview1-final

any help @bricelam

bricelam commented 7 years ago

It sounds like you may need to add an implementation of IDbContextFactory to your assembly. In version 2.0.0-preview1-final, we temporarily stopped using the ASP.NET Core application service provider to discover the DbContext. Have you considered upgrading to version 2.0.1?

IsaaasI commented 7 years ago

Many Thanks bricelam for your fast replay. I updeted the package to v2.0.1 and I face the below issue. Actually, If possible to contact with you on Skype and I will share my screen with you to solve the problem faster . image

Installed Packages & SDK image

ralmsdeveloper commented 7 years ago

@IsaaasI Remove the Microsoft.EntityFrameworkCore.SqlServer.Design dependency, it is not used in version 2.0.

Also remove the Entity Framework 6.2 from your project!

Entity Framework 6 does not support .NET Core

smitpatel commented 7 years ago

Also upgrade Microsoft.AspNetCore.All to 2.0.3 version.

IsaaasI commented 6 years ago

@bricelam Still same issue. required your help. @ralmsdeveloper I deleted both Microsoft.EntityFrameworkCore.SqlServer.Design & Entity Framework 6.2 and still I have below issue. image

@smitpatel I tried many times to update to 2.0.3 but no luck . below the issue. I already cleared the cache for NuGet and same thing. image

ralmsdeveloper commented 6 years ago

@IsaaasI, Do this manually, edit your * .csproj file.

Search for: <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview1-final" />

and replaced by: <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />

Also do this for System.Text.Encodings.Web, the stable version is 4.4.0.

IsaaasI commented 6 years ago

@ralmsdeveloper I am not able to find the .csproj from the Solution Explorer. I think the structure for VS 2017 and .NET core 2.0 application is different than previous version. image

I open it from Window explorer by notepad image I found these statements. image

ralmsdeveloper commented 6 years ago

Check this file.

32956041-bf7a5548-cbc8-11e7-950b-9f11b12bb0cf

IsaaasI commented 6 years ago

Yes , True

I fix it but still the migration not work :)

Can I chat with you on any chatting application to solve migration issue? I want only connect .NET Core2.0 app with MYSQL Database by entity framework .

@ralmsdeveloper @bricelam

ralmsdeveloper commented 6 years ago

Are you installing the Microsoft.EntityFramework.SqlServer package and want to try connecting to MySQL?

You will not, wrong package!

Try using MySql.Data.EntityFrameworkCore.

bricelam commented 6 years ago

You may need to update Program.cs too.

IsaaasI commented 6 years ago

It is already installed , are you sure the Entity Framework 6.2 not required ? Brcasue the below error is I think required Entity framwork. No DbContext was found in assembly '***'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

I delete SqlServer @ralmsdeveloper

I will try update Program.cs now @bricelam

IsaaasI commented 6 years ago

The program.cs is already updated. image

@bricelam

ralmsdeveloper commented 6 years ago

Post your DbContext here.

IsaaasI commented 6 years ago

image

image

This is my error now. image

ralmsdeveloper commented 6 years ago

I think it would be ideal, you see some articles before, to help you better understand, how to use EF Core:

https://docs.microsoft.com/en-us/aspnet/core/tutorials/

https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/crud

smitpatel commented 6 years ago

@IsaaasI - Your project has errors. Before running migrations make sure you can build you project in VS without errors.

IsaaasI commented 6 years ago

Thanks a lot @ralmsdeveloper . Finally, I did it . The Migration work fine now. But :) 1.enable-migrations -> works fine. 2.Add-Migration -> works fine image 3.update-database -> works fine image

But I didn't see the table created on my localhost database.

This is my connection string. I am wondering about my schema name , username and password of my database where I put it? image

Remark: I am using MySQL Workbenck 6.3 CE image

@smitpatel Yes, It seem like that . The issue is solved and migration work fine but remaining above issue.

ralmsdeveloper commented 6 years ago

Its Context is configured to use SQL Server and not MYSQL, so it is impossible to see the tables!

It would be very important to study the documentation!

https://docs.microsoft.com/en-us/ef/core/

or some article that shows how to use Asp.Net Core + EF Core

It looks like you're a little confused :)

I'm sure you can do that.

It would be important for you to search for articles on the internet, there are several good blogs for studies!

one of them is: https://channel9.msdn.com/

The EF team is wonderful, they are super attentive, they always try to respond in the best possible way.

But your case is a bit quirky, because here they usually discuss improvements or bugs in EF.

ralmsdeveloper commented 6 years ago

@ajcvickers