Unable to scaffold “datetime” type columns from MySQL in .net core 3.1 - mysql

A similar issue has been posted in the past about scaffolding in .net core 2 but that was on a date column and no solution has been posted to date.
There's no specific table as any table containing a datetime column type throws the following error from the console:
Could not find type mapping for column 'database.table.columnname' with data type 'datetime'. Skipping column.
This answer to a question about reading datetime values and a MySQL quirk with 0000-00-00 00:00:00 values made me wonder if changing the connection string format would help. So I added the ConvertZeroDateTime=true to the connection string in the scaffold command:
Scaffold-DbContext "server=localhost;port=3306;user=root;password=notherealpassword;database=sodb;ConvertZeroDateTime=true;" MySql.Data.EntityFrameworkCore -OutputDir Model -f
I still have the same error, does anyone have any suggestions? I have 380+ tables in this database so an automated solution would be most helpful.

I had the same problem and now I solved it.
I can't be assured if my solution works to your case but I think this solution would work to your case.
Remove all packages related to Entity Framework.
Install the following packages the version is 5.0.4
Open Package Manager Console window. Now we will install Pomelo packages it depends on .Net standard.
Enter the command “Install-Package Pomelo.EntityFrameworkCore.MySql -Version 5.0.0-alpha.2”
Enter the command "Install-Package Pomelo.EntityFrameworkCore.MySql.NetTopologySuite -Version 5.0.0-alpha.2"
The commands try to install the following packages.
Now Enter to Package Manager Console window the following command.
Scaffold-DbContext "server=localhost;port=3306;uid=root;pwd=notherealpassword;database=sodb; "Pomelo.EntityFrameworkCore.MySql" -OutputDir Models -f
The result of command is as below.
I hope this post helps you.

I was scaffolding off mySql 5.7 in .net core 2.2 using pomelo no problem until i tried upgrading to 3.1
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.6">
<PackageReference Include="MySql.Data" Version="8.0.21" />
<PackageReference Include="MySql.Data.EntityFrameworkCore.Design" Version="8.0.19" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.6" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
This combination worked for me calling
PM> Scaffold-DbContext [ConnectionStringHere] MySql.Data.EntityFrameworkCore -OutputDir [NameHere] -Project "[NameHere]" -Force
With the below packages, scaffolding just skips datetime completely (also bytes are converted to bools)
So to workaround. I used my source control to keep the old datetime classes and just manually updated the bools. You could try downgrading to older versions to see if helps.
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.11"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11">
<PackageReference Include="MySql.Data" Version="8.0.23" />
<PackageReference Include="MySql.Data.EntityFrameworkCore.Design" Version="8.0.19" />
<PackageReference Include="NonFactors.Grid.Mvc6" Version="6.1.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.4" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />

I have achieved a fix for this today with the following Nugets: -
And then in Package Manager Console of Visual Studio: -
Scaffold-DbContext "server=MYSERVER;port=3306;database=MYDB;user=MYUSER;password=MYPW;" MySql.EntityFrameworkCore -UseDatabaseNames -f
The "-UseDatabaseNames" and "-f" flags are not essential to the bare fix, but that's what I'm using.
The crucial detail is that MySql.EntityFrameworkCore v5.0.3 works where v5.0.0 does not.

I was getting datetime/timestamp erros like yours, I must had to use those packages to made to work
mysql.entityframeworkcore isnt necessary
And the command to export is
dotnet ef dbcontext scaffold "Database=#dbname;Data Source=#ip:#port;User Id=#user;Password=#pass;Initial Catalog=#dbname;" pomelo.entityframeworkcore.mysql -o Models -f -c DBContext

Related

Unable to resolve service for type Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger

I am having difficulties to scaffold an existing MySQL database using EF core.
I have added the required dependencies as mentioned in the oracle doc:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkcore.Tools" Version="6.0.0">
and then, I ran this code in the package manager console:
Scaffold-Dbcontext "server=the.server.ip.address;user=user_name;database=db_name;password=db_password;port=3306" MySql.EntityFrameworkCore -o Data -v
It shows this error:
Unable to resolve service for type
'Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger`1[Microsoft.EntityFrameworkCore.DbLoggerCategory+Scaffolding]'
while attempting to activate
'MySql.EntityFrameworkCore.Scaffolding.Internal.MySQLDatabaseModelFactory'
Here are the relevant logs in the output window:
Finding design-time services referenced by assembly 'Test2'...
Finding design-time services referenced by assembly 'Test2'...
No referenced design-time services were found.
Finding design-time services for provider 'MySql.EntityFrameworkCore'...
Using design-time services from provider 'MySql.EntityFrameworkCore'.
Finding IDesignTimeServices implementations in assembly 'Test2'...
No design-time services were found.
I don't know how shall I implement the design time classes and nor did I find any useful links in the web.
Note that I can access and run query on the database using MySQL Workbench.
I got this error not while scaffolding but when trying to create my first migration. I didn't want to downgrade to 5.0 because it would've had to be permanent since I was going to run a lot of migrations.
I fixed it by changing my provider from MySql.Data.EntityFrameworkCore to Pomelo.EntityFrameworkCore.MySql
Remove the old provider from both my API and DAL projects:
dotnet remove package MySql.EntityFrameworkCore
Add Pomelo provider
dotnet add package Pomelo.EntityFrameworkCore.MySql
Update your startup to use Pomelos configuartion:
https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#2-services-configuration
Migrations are working:
dotnet ef migrations add InitialCreate
According this description
https://bugs.mysql.com/bug.php?id=106592
you can solve this error by adding below class to your code
public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
serviceCollection.AddEntityFrameworkMySQL();
new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
.TryAddCoreServices();
}
}
and after define you have to add it as service in startup file
like this
services.AddSingleton<IDesignTimeServices, MysqlEntityFrameworkDesignTimeServices>();
After these steps you can migrate your code :)
I came across the same issue trying to scaffold an existing MySQL database. It looks like the latest version of MySql.EntityFrameworkCore (6.0.0-preview3.1) still uses the EFCore 5.0 libraries and has not been updated to EFCore 6.0.
It also seems Microsoft.EntityFrameworkCore.Diagnostics was last implemented in EFCore 5 and removed in 6.
When I downgraded all the packages to the 5 version level, I was able to run the scaffold command without that error.
To make Scaffold-Dbcontext work, I had to downgrade both the packages to 5.0.0 version.
MySql.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools
After successful scaffolding, I upgraded these packages back to the their latest versions.
I could find the Core 6.0 documentation for microsoft.entityframeworkcore.diagnostics in the official website, but it was still not working when I tried the Scaffold-DbContext command. Had to downgrade all the packages to the last 5.0 version before it worked. Here are the packagerefs in my project settings
"Microsoft.EntityFrameworkCore.Design" Version="5.0.13"
"Microsoft.EntityFrameworkCore.Tools" Version="5.0.13"
"MySql.Data" Version="8.0.28"
"MySql.EntityFrameworkCore" Version="5.0.10"
#https://stackoverflow.com/users/1322226/quinestor I faced same issue and as #https://stackoverflow.com/users/9914700/james-ruth mentioned I downgraded the versions of all EFCore and EFCore Design to 5.0.8 in Visual Studio.
Did not look around for command line commands :) But we can also do it from from dotnet cli, which I guess you probably would be aware of. We can remove - dotnet remove package <PACKAGE_NAME>, and install specific version - dotnet add package <PACKAGE_NAME> --version
You do not need to perminately downgrade your EF version to scaffold, you can edit the project file to use
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MySql.EntityFrameworkCore" Version="5.0.8" />
</ItemGroup>
Then you can run
dotnet ef dbcontext scaffold "[DSN String]" MySql.EntityFrameworkCore -o [DBName]
Then change your project file back (or revert changes) to use the latest version of EF.
Dont forget to edit the "protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)" and remove the DSN it adds to the code.
The latest version of MySql.EntityFrameworkCore still uses the EFCore 5.0 libraries and has not been updated to EFCore 6.0.
It also appears that Microsoft.EntityFrameworkCore.Diagnostics was removed in 6.
I resolve this error by adding the class below to my code:
public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
serviceCollection.AddEntityFrameworkMySQL();
new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
.TryAddCoreServices();
}
}
Then you must add it as a service in the initialization file
services.AddSingleton<IDesignTimeServices, MysqlEntityFrameworkDesignTimeServices>();
Hope this helps!

Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project

I've been researching a lot online but did not find a proper solution. I was trying to use Entity Framework Core with MySQL by using database-first scaffold method to mapping table model while always received this error when applied the command
Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project. If you're using custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.
This is the command I am using to scaffold the database model:
Scaffold-DbContext "server=localhost;port=3306;user=root;password=1234;database=world" "Pomelo.EntityFrameworkCore.MySql" -OutputDir .\Models -f
And this is my .Net Core project setting:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
For EF Core 3
I think the powershell command has gone away for EF Core 3 so I'm using dotnet ef dbcontext scaffold
With this command, if you're converting a Scaffold-DbContext script the project name (parameter --project) needs to have .csproj on the end - it can't just the extensionless name.
I got this error trying to add a new migration on the command line with dotnet ef migrations add NewMigration.
The solution for me was indeed to append --msbuildprojectextentionspath obj/local.
For my case this error was solved by downloading latest dotnet-sdk-2.2.107-win-x64
for VS2017 and then dropping and updating database through cmd:
dotnet ef database drop --force --context <<dbcontext>> -s <<startup project directory>> -p <<project directory>>
dotnet ef database update --context <<dbcontext>> -s <<startup project directory>> -p <<project directory>>
Here goes the definitively solution for this problem.
I work with Rider Ide constructing a Web Api, using Net core 3 with Pomelo.MySql
I have tree layers defining my application:
Test.Domain - Class Library defining my models
Test.Infra.DataAccess - Class library defining my DbContext
Test.Service.RestApi - Net Core Api Project, where I define the AppContext in Startup file. Like This:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AerjDbContext>(
options => options.UseMySql(Configuration.GetConnectionString("MySqlConnectionString"),
mysqlOptions => mysqlOptions.ServerVersion(new Version(8,0,19), ServerType.MySql)
.EnableRetryOnFailure()
));
}
This is quite normal, the magic occurs here:
Initiate migrations:
dotnet ef migrations add InitialCreate --startup-project "../<Here the name of the project where startup file was configured>"
It'll create the migrations
then we have to update database with sabe command structure;
dotnet ef database update --startup-project "../<Here the name of the project where startup file was configured>"
I have tried different solutions to solve this error. Did following change in .csproj file.
Solved this issue for .Net Core by edit .csproj file and removed Import tag and then run ef command again in command line.
EF migrations with .NETCore 2.2 can be smoke-tested using migrations list:
dotnet ef migrations list --startup-project <app-startup-project-dir> --project <dal-project-dir> --context App.DAL.ApplicationDbContext
--context must point to the fully qualified name of the DbContext class that you want to manipulate.
Add this parameter to the end of your scaffold command "--msbuildprojectextensionspath".

WP8 updated to Googles API to v3 reference assemblie error

Hi I have updated my project references to include the latest Goolges.apis.Youtube.v3 library which I downloaded from NuGet using the console manager in Visual Studio 2013. Now when I compile the project I get the following error:
Error 1 Cannot resolve reference assemblies. Please check the reference assemblies. Cannot resolve dependency to assembly 'Google.Apis, Version=1.9.0.23042, Culture=neutral, PublicKeyToken=null' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.
I have tried right clicking references > manage nuget packages and removing Goolges.apis.Youtube.v3 which also removes some dependencies etc.
I have done a clean bulild between uni-stalls and aslo deleted the following items from the packages.config file in the project directory:
<package id="Google.Apis" version="1.9.0" targetFramework="wp80" />
<package id="Google.Apis.Auth" version="1.9.0" targetFramework="wp80" />
<package id="Google.Apis.Core" version="1.9.0" targetFramework="wp80" />
<package id="Google.Apis.YouTube.v3" version="1.9.0.1280" targetFramework="wp80" />
As they still resided there after the unisatll..?
Any help with is much appreciated?
Thanks

Developer Licence error when running WinRT unit tests using vstest.console.exe from the command line

I'm getting the following error when I try to execute WinRT MSTests from the command line:
EXEC : error : Could not start test run for unit tests for Windows Store app: No valid developer license found for running unit tests for Windows Store apps. Please install/renew your developer license..
This used to work, but has suddenly started failing. The odd thing is that they execute fine from within visual studio.
I'm using the following MSBuild task.
<Target Name="UnitTest" DependsOnTargets="Compile" >
<ItemGroup>
<TestAppx Include="$(SolutionDir)\**\*x86*\**\*Tests*.appx" />
</ItemGroup>
<Message Importance="high" Text="Running tests for %(TestAppx.Identity)" />
<Exec Command='"$(VSINSTALLDIR)\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" %(TestAppx.Identity) /InIsolation /platform:x86 /Logger:trx /UseVsixExtensions:true'
WorkingDirectory="$(SolutionDir)"/>
</Target>
Still not exactly sure why VisualStudio and vstest were out-of-sync, but I found a solution.
Open PowerShell as an administrator
Execute: Unregister-WindowsDeveloperLicense
Open the solution in visual studio
Log in to your live account when prompted to re-create your developer licence
Source: http://msdn.microsoft.com/en-us/library/windows/apps/hh974578.aspx#getting_a_developer_license_at_a_command_prompt

phing with phpdoc2 doesn't run

So I have a jenkins job to update a copy of my code and generate the phpdoc for my library, this is all done with phing
When I use these line in de build.xml it generates jsut fine (but with phpdocumentor 1.4.4)
<target name="phpdoc">
<echo msg="PHP Documentor..." />
<phpdoc title="API Documentation"
destdir="/var/www/corelib"
sourcecode="yes"
output="HTML:Smarty:PHP">
<fileset dir="./library/Core">
<include name="**/*.php" />
</fileset>
</phpdoc>
</target>
I want to use the new version of phpdocumentor so i installed it with pear
pear install phpdoc/phpDocumentor-alpha
But when I run this command (I found this in the phing docs), jenkins prints "PHP documentor" and then directly marks the build as failed
<target name="phpdoc">
<echo msg="PHP Documentor..." />
<phpdoc2 title="API Documentation" destdir="/var/www/corelib" template="responsive">
<fileset dir="./library/Core">
<include name="**/*.php" />
</fileset>
</phpdoc2>
</target>
I have zendserver installed on this server but that can't be a problem because phpdoc 1.4.4 runs fine
So how do i solve this?
Ok i found the solution, so i post it for future reference
With some help of the people on the irc channel of phpdocumentor
so this a bug with the latest build of phing in combination with the latest version of phpdocumentor. To fix this problem just revert the instalation of phpdocumentor2 to this version
phpDocumentor 2.0.0a3
This will fix the problem with phing and the documentation gets generated with no problem