Micro blog about Answer to the Ultimate Question of Life, the Universe, and Everything.
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Entity framework Core MySql scaffolding example

Details
Written by: Stanko Milosev
Category: C#
Published: 27 February 2021
Last Updated: 07 November 2021
Hits: 3503
  • core
Mostly I was using this web site for my example.

In my case I have created the project C:\projects\ReverseGeoCoding, then open PowerShell, and first, if you don't have EF tool installed, execute following:

dotnet tool install --global dotnet-ef
Go to project folder, like cd "C:\projects\ReverseGeoCoding", and add packages "Microsoft.EntityFrameworkCore.Design" and "MySql.EntityFrameworkCore" like:
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package MySql.EntityFrameworkCore --version 5.0.0-m8.0.23
Now scaffold:
dotnet ef dbcontext scaffold "SERVER=localhost;DATABASE=myDb;UID=uid;PASSWORD=pass;" MySql.EntityFrameworkCore -o C:\projects\ReverseGeoCoding -f
Now, I have made my own console applicatcation for scaffolding:
using System.Diagnostics;
using System.Threading;

namespace MySQLScaffold
{
  class Program
  {
    static void Main(string[] args)
    {
      // Use ProcessStartInfo class
      ProcessStartInfo startInfo = new ProcessStartInfo
      {
        CreateNoWindow = false,
        UseShellExecute = false,
        FileName = "dotnet",
        WindowStyle = ProcessWindowStyle.Hidden,
        Arguments = "ef dbcontext scaffold"
                    + " \"SERVER=localhost;DATABASE=myDb;UID=uid;PASSWORD=pass;\""
                    + " MySql.EntityFrameworkCore -o"
                    + " C:\\projects\\ReverseGeoCoding"
                    + " -f -v"
                    + " -p C:\\projects\\ReverseGeoCoding"
      };

      Mutex myMutex;
      if (!Mutex.TryOpenExisting("testMutex", out myMutex))
      {
        myMutex = new Mutex(true, "testMutex");
        myMutex.WaitOne();
        try
        {
          // Start the process with the info we specified.
          // Call WaitForExit and then the using statement will close.
          using (Process exeProcess = Process.Start(startInfo))
          {
            exeProcess?.WaitForExit();
          }
        }
        catch
        {
          // Log error.
        }
        finally
        {
          myMutex.ReleaseMutex();
        }
      }

      //Console.WriteLine("Press any key");
      //Console.ReadKey();
    }
  }
}
Then I have created Class library application "EFCore", where in prebuild event I have added
C:\projects\MySQLScaffold\MySQLScaffold\bin\Debug\netcoreapp3.1\MySQLScaffold.exe
I had to add package "Microsoft.EntityFrameworkCore.Design", and I had to reference my "ReverseGeoCoding" project, here is how my "EFCore.csproj" look like:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="C:\projects\MySQLScaffold\MySQLScaffold\bin\Debug\netcoreapp3.1\MySQLScaffold.exe" />
  </Target>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\ReverseGeoCoding\ReverseGeoCoding.csproj" />
  </ItemGroup>

</Project>

Pre-build events

Details
Written by: Stanko Milosev
Category: C#
Published: 12 February 2021
Last Updated: 12 February 2021
Hits: 2936
  • visual studio
In order to save output of your Pre-build Events write something like:

echo ProjectDir=$(ProjectDir) >>$(TEMP)\macros.txt
Then open file %temp%\macros.txt

List of Macros you can see in Edit Pre-Build -> Macros >>

Split string in four blocks

Details
Written by: Stanko Milosev
Category: C#
Published: 05 October 2020
Last Updated: 05 October 2020
Hits: 3385
  • regular expressions
One example on how to display IBAN in four blocks:
Regex.Replace("DE89370400440532013000", ".{4}", "$0 ");

LINQ to XML duplicated Node

Details
Written by: Stanko Milosev
Category: C#
Published: 12 September 2020
Last Updated: 15 September 2020
Hits: 3168
  • xml
For example you want to extract from XML Key - Value - Pair. First "value" node is key, second "value" node is value. Example XML:
<params>
	<param>
		<value>
			<array>
				<data>
					<value>
						<string>NameOfValueOne</string>
					</value>
					<value>
						<string>ValueOne</string>
					</value>
				</data>
			</array>
		</value>
	</param>
	<param>
		<value>
			<array>
				<data>
					<value>
						<string>NameOfValueTwo</string>
					</value>
					<value>
						<string>ValueTwo</string>
					</value>
				</data>
			</array>
		</value>
	</param>
</params>
Code:
Dictionary<string, string> myDict = new Dictionary<string, string>();
XElement myXML = XElement.Load("xml.xml");
IEnumerable<XElement> xElementData = from data in myXML.Descendants("data") select data;
foreach (XElement xElement in xElementData)
{
	myDict[(string)xElement.Elements().ElementAt(0)] = (string)xElement.Elements().ElementAt(1);
}

foreach (KeyValuePair<string, string> keyValuePair in myDict)
{
	Console.WriteLine($"Key: {keyValuePair.Key}, value: {keyValuePair.Value}");
}

Console.WriteLine("Press any key");
Console.ReadKey();
POI:
IEnumerable<XElement> xElementData = from data in myXML.Descendants("data") select data;
foreach (XElement xElement in xElementData)
{
	myDict[(string)xElement.Elements().ElementAt(0)] = (string)xElement.Elements().ElementAt(1);
}
Source download from here.
  1. Detect the encoding/codepage of a text file
  2. Read file in chunks
  3. List.Contains
  4. JSON configuration file in .NET core

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 11 of 163

  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15