UseMySQL extension method not recognized in ASP.NET core - entity-framework

Environment: Ubuntu 16.04, .NET Core 1.10 (1.0.0-preview2-1-003177), Visual Studio Code 1.8.1
I have created an ASP.NET MVC Core application by running the following command:
$ dotnet new -t web
I am able to load the folder in VSC and debug it.
By default, the engine generates code for Sqlite. I am changing it to use MySQL. My changes are based on the information from the following two articles:
http://insidemysql.com/howto-starting-with-mysql-ef-core-provider-and-connectornet-7-0-4/
https://damienbod.com/2016/08/26/asp-net-core-1-0-with-mysql-and-entity-framework-core/
First, I added the following lines into dependencies section of project.json.
"MySql.Data.Core" :"7.0.4-ir-191",
"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"
After running dotnet restore, the required DLLs were downloaded.
The next step was to modify Startup.cs and modify
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
to
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
Essentially, I am replacing UseSqlite by UseMySQL.
However, extension method UseMySQL or UseMySQL do not seem to be available on DbContextOptionsBuilder.
Wondering if I missed some step somewhere. Regards.

Add the using MySql.Data.EntityFrameworkCore.Extensions; statement.

I've successfully used this in my Mac with the latest version of VS Community in to an ASP CORE MVC project and I just needed to add the following NuGet-Packages:
Microsoft.EntityFrameworkCore
MySql.Data.EntityFrameworkCore
Then using them on your class like:
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
Notice the MySQL on the USING statement is CAPITAL letter, and it is MySql on the NuGet Package name.
Hope this hel

I also had the same issue even though was installing the Pomelo.EntityFrameworkCore.MySql,
just installed the package through the terminal:
dotnet add package Pomelo.EntityFrameworkCore.MySql
it worked!

Related

How can I call Add-Migration without parameters

I have .net Core 3.1 solution with WebApi and Persistance projects. WebApi is the solution's startup project, Persistance is where DbContext is defined. After a lot of reading and trying stuff, I still cannot achieve the following:
WebApi stays as the solution's startup project.
Migrations folder with all the migrations classes is in the Persistance project.
Migrations can be generated from VS Package Manager Console by typing Add-Migration <migration_name> without any additional parameters like -Project or -StartupProject.
No need to change selection in the Package Manager Console Default project dropdown.
I wanted to achieve the same result but on MAC OS. Because Visual Studio on Mac is not like Window's, We use CLI commands. In my case I had Migration Classes in another project and startup was Api project just like you so i added this to my terminal :
alias migrationfortest='dotnet ef migrations add $1 --project /Users/user/Projects/test-web/Test.Infrastructure'
and you can call this simply by typing migrationfortest "YOURMIGRATIONNAME"
NOTE : For CLI command tools you should add This

EF Migration: Could not load assembly. Ensure it is referenced by the startup project after switching from x64 to x86

After switching the all the projects to x86 architecture, I get the following error:
> dotnet ef migrations add Nullable-Fields --startup-project ..\LairageScanner.BlazorApp
Build started...
Build succeeded.
Could not load assembly 'LairageScanner.Context.SQL'. Ensure it is referenced by the startup project 'LairageScanner.BlazorApp'.
As seen above I have tried to run the command through CMD - but also done this for the package manager. I have seen some suggestion to fix this error: Stack Overflow & GitGub but neither of them had the solution to my issue.
Current Stack:
EF Core 3.1.3
Dotnet Core 3.1
Note: From the Git Solution it suggest converting to x64 and then just converting back later. And it does work. But I am looking for a more permanent solution.
I fixed it once with a simple action.
You need to be sure that the Startup Project is the one with the startup.cs file. Also, be sure that all Project references are complete.
Well, it worked for me.
Select the layer where the dbcontext class exists

Rider. EF Code First Migrations

Is there an easy way to scaffold migrations using old EF outside of Visual Studio? I would like to do it via Rider IDE if it possible.
I don't see these options related to ef core. The plugin makes sense.
(For MacOS, be sure, your dotnet root folder path is /usr/local/share/dotnet/. I'm telling this because Rider installs and places it in another folder. Further it gives rise ef to not work properly. You can see your dotnet folder on terminal by writing which dotnet)
Installing that plugin,
Then,
You can also use a jetbrains plugin made for handling migrations
https://plugins.jetbrains.com/plugin/17026
For EF Core, you can use https://blog.jetbrains.com/dotnet/2017/08/09/running-entity-framework-core-commands-rider/
For EF 6 you may want to check https://blog.jetbrains.com/dotnet/2018/04/06/entity-framework-support-rider-2018-1/
The Package Manager Console tools such as Add-Migration, Scaffold-DbContext commands are PowerShell-based, and the Package Manager Console ties to several Visual Studio-specific objects making it impossible to host it elsewhere - in your case Rider.
In Rider's terminal or anywhere outside of Visual Studio, you can use CLI tools. Equivalents to the highlighted commands would be respectively:
Add-Migration => dotnet ef migrations add MigrationName
Scaffold-DbContext => dotnet ef dbcontext scaffold
You can get more details on JetBrains blog: Running EF Core commands in Rider

Calling AS400 DB from .Net Core

I have a .Net Core WebAPI, and I have the IBM.Data.DB2.Core package installed.
When I try to call the DB2Connection constructor with a connection string, I'm getting the following error.
Unable to load DLL 'db2app64.dll': The specified module could not be found.
Is this something that should be included with the DB2.Core package? Is there a better way to call an AS400 DB from .Net Core?
My code looks like this:
DB2Connection conn = new DB2Connection("Server=as400.example.com;Database=CLIENTS01;UID=user1;PWD=password1;");
If you are using the .NET Core package, you will have to include the path to the driver (which is included in the NuGet package) in your PATH variable.
Here's the relevant part of the FAQ on the NuGet package on Developer Works:
Q: Do I need to do any additional configuration for using this
configuration.
Yes, On Windows :
if C:\Users\<USERNAME>\ is the NuGet package folder then
add to the path: C:\Users\<USERNAME>\.nuget\packages\IBM.Data.DB2.Core\1.0.0.100\build\clidriver\bin
and On Linux:
append $HOME/.nuget/packages/IBM.Data.DB2.Core-lnx/1.0.0.100/build/clidriver/lib
to the LD_LIBRARY_PATH.
I've found the easiest way to get it set is to set the PATH variable in your project settings. Check out this blog post about some different ways you can do that.
In fact, this other Developer Works page says:
Instructions for downloading and using the package
The following are the prerequisites for using the package
Any other IBM DB Drivers should not be present in the machine.
Path/LD_LIBRARY_PATH needs to be updated to include the package driver path.
you need to install the IBM Data Server Driver Package wich you can get over there
http://www-01.ibm.com/support/docview.wss?uid=swg24038920
it includes the dependencies you are missing

How can I run an Entity Framework 7 migration on a remote database using Powershell?

In a standalone entity framework 7 project (note, not a MVC project with an entity context where the command DNX might be used), how can I run a migration on a remote database using Powershell?
I am currently using Entity Framework 7.0.0-rc1-final with Visual Studio 2015 (14.0.23107.0).
At the moment there is no way you can use pure PowerShell to do this because a utility like migrate.exe does not exist yet and importing the EF PS modules is not possible as they require a Package Manager PowerShell Host.
Here are some ideas how you can update a remote db in EF7:
One thing you could do is use the package manager console commands from within VS as usual to update the remote db. You can create a second context that has the remote db connection string and use the update-database command specifying the context to use. These commands require the following package in EF7:
https://www.nuget.org/packages/EntityFramework.Commands/.
I have done this successfully in a class lib project.
Another solution would be to use DNX commands by creating a DNX project instead of a classic one. DNX projects are not just for web sites, it is just another type of project. Here is a link that shows how to create a console app DNX project:
http://docs.asp.net/en/latest/dnx/console.html.
So with this type of project you can use the provided DNX commands that you seem to be aware of.
I hope this helped. Maybe we can give more help if you describe your situation and your end goal in more detail.
Answer too long as a comment, so adding it here...
Have you looked at this article and the links in the answer?
From that answer
The problem with importing the module into a PowerShell console is that I believe the module expects to run in a context where it has a Visual Studio DTE object available. That environment is the NuGet Package Manager Console. This issue has been brought up before. Check out this blog post and this SO question.
This blog post shows how to write code that does migrations.
What might be helpful for readers of this question is what you have tried, what is not working, and other information that might help solve your problem.