@manhng

Welcome to my blog!

Using Scaffold-DbContext in EF Core 2.1 with Firebird database

April 17, 2021 22:03

Using Scaffold-DbContext in EF Core 2.1 with Firebird database (edit)

Currently supported by EF Core:

  • Microsoft SQL Server
  • SQLite
  • Postgres (Npgsql)
  • SQL Server Compact Edition
  • InMemory (for testing purposes)
  • MySQL
  • IBM DB2
  • Oracle
  • Firebird

Forcus on:

  • .NET Core 2.1
  • EF Core 2.1
  • Firebird Database
  • Scaffold-DbContext
  • Generate Models from Existing Database

Nugets:

  • .NET Core 2.1
  • ASP.NET Core MVC 2.1
  • Install-package EntityFrameworkCore.FirebirdSql -Version 2.1.2.2
  • Install-package Microsoft.EntityFrameworkCore -Version 2.1.14
  • Install-package Microsoft.EntityFrameworkCore.Tools -Version 2.1.14

EF 6:

  • EntityFramework (EF 6)
  • EntityFramework.SqlServerCompact (EF 6)

EF Core:

  • Microsoft.EntityFrameworkCore
  • FirebirdSql.Data.FirebirdClient
  • FirebirdSql.EntityFrameworkCore.Firebird

.NET Provider for Firebird

  • Firebird ADO.NET Data Provider
  • Microsoft SQL Server Compact Data Provider 4.0
  • More ...

How to build a Connection String?

Class: FirebirdSql.Data.FirebirdClient.FbConnectionStringBuilder

var connectionString = new FbConnectionStringBuilder
{
Database = "mydb",
DataSource = "localhost",
ServerType = FbServerType.Default,
UserID = "sysdba",
Password = "masterkey",
}.ToString();

How to use Scaffold-DbContext?

Scaffold-DbContext "character set=none;data source=localhost;initial catalog=mydb;user id=sysdba;password=masterkey;" EntityFrameworkCore.FirebirdSql -Force -OutputDir Models

Scaffold-DbContext "User=xxxx;Password=xxxx;Database=xxxx;DataSource=xxxxxx;Port=3050;Dialect=3;Charset=NONE;Role=;Connection lifetime=15;Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;" FirebirdSql.EntityFrameworkCore.Firebird -OutputDir Models

.NET Core CSharp Project (.csproj)

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EntityFrameworkCore.FirebirdSql" Version="2.1.2.2" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project> 

Next with .NET Core 3.1:

  • .NET Core 3.1
  • ASP.NET Core MVC 3.1
  • EF Core 3.1

Next with .NET Core 5.0:

  • .NET Core 5.0
  • ASP.NET Core MVC 5.0
  • EF Core 5.0

References:

https://github.com/cincuranet/FirebirdSql.Data.FirebirdClient

https://github.com/ralmsdeveloper/EntityFrameworkCore.FirebirdSQL (HAY HAY HAY)

https://www.programmersought.com/article/6109272731/ (HAY HAY HAY)

https://hoanguyenit.com/create-database-using-code-first-in-aspnet-core-21.html

https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-5.0

http://www.binaryintellect.net/articles/87446533-54b3-41ad-bea9-994091686a55.aspx

https://docs.oracle.com/cd/E17952_01/connector-net-en/connector-net-entityframework-core-example.html

Domain Driven Design Implement

March 8, 2021 21:56

Domain Driven Design Implement (edit)

Domain Driven Design Implementation Approach with Generic Repository and UoW Pattern in ASP.NET Core 3.1 Web API and EF Core 5.0

https://www.codeproject.com/Articles/5296451/Domain-Driven-Design-Implementation-Approach-with

https://github.com/tomajexpress/Domain.Driven.Implementation.In.CSharp.NET.Core

  • Domain Driven Design
  • Generic Repository
  • Unit of Work
  • ASP.NET Core 3.1
  • ASP.NET Core 3.1 Web API
  • SQL Server
  • EF Core 5.0
  • Database Migrations
  • DbContext
  • IUnitOfWork, IMapper (AutoMapper)
  • Unit Test (NUnit)

Image 1

Work with EFCore Dapper together in the PostgreSQL database

March 5, 2021 08:42

Work with EFCore Dapper together in the PostgreSQL database (edit)

Install-Package Dapper
Install-Package Npgsql

Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.Design
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Relational
Install-Package Dapper
Install-Package Npgsql
Install-Package System.Data.SqlClient

https://codewithmukesh.com/blog/using-entity-framework-core-and-dapper/ (HAY)

https://github.com/iammukeshm/EFCoreAndDapper

https://dotnetcoretutorials.com/2020/07/11/dapper-with-mysql-postgresql-on-net-core/

https://www.c-sharpcorner.com/article/getting-started-with-postgresql-using-dapper-in-net-core/

https://techbrij.com/asp-net-core-postgresql-dapper-crud/ (HAY)

https://dotnetcorecentral.com/blog/postgresql-and-dapper-in-net-core/

Oracle + Entity Framework Core

January 31, 2021 09:13

Entity Framework Core + Oracle (edit)

  1. Update Model from Database...
  2. Generate Database from Model...

Entity Framework, LINQ and Model-First for the Oracle Database

https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/EntityFrameworkOBE/EntityFrameworkOBE.htm

Entity Framework Core tools reference - .NET Core CLI

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

dotConnect for Oracle

https://www.devart.com/dotconnect/oracle/articles/efcore-database-first-net-core.html

Starting with an existing database

https://www.learnentityframeworkcore.com/walkthroughs/existing-database

Oracle DB First

https://www.devart.com/dotconnect/oracle/articles/efcore-database-first-net-core-entity-developer.html

Oracle Command - Inserting Data in Run Time

https://www.devart.com/dotconnect/oracle/articles/tutorial-command.html

To insert the first row into table dept you can use the following statement:

  1. CREATE TABLE dept:

    CREATE TABLE dept (
      deptno INT PRIMARY KEY,
      dname VARCHAR(14),
      loc VARCHAR(13)
    )
    
  2. CREATE TABLE emp:

    CREATE TABLE emp (
      empno INT PRIMARY KEY,
      ename VARCHAR(10),
      job VARCHAR(9),
      mgr INT,
      hiredate DATE,
      sal FLOAT,
      comm FLOAT,
      deptno INT REFERENCES dept
    )
INSERT INTO dept (deptno, dname, loc) VALUES (10,'Accounting','New York')

The following code fragment executes the query:

OracleConnection conn = new OracleConnection("User Id=scott;Password=tiger;Server=OraServer;");
OracleCommand cmd = new OracleCommand();
cmd.CommandText = "INSERT INTO dept (deptno, dname, loc) VALUES (10,'Accounting','New York')";
cmd.Connection = conn;
conn.Open();
try {
  int aff = cmd.ExecuteNonQuery();
  MessageBox.Show(aff + " rows were affected.");
}
catch {
  MessageBox.Show("Error encountered during INSERT operation.");
}
finally {
  conn.Close();
}

Console Application - How You Can Create a .NET Core Application Using Entity Framework Core with Oracle

https://www.talkingdotnet.com/create-net-core-application-using-entity-framework-core-with-oracle/

  1. Update Model From Database...
  2. Update Database from Model...
  3. Generate Database Script From Model...

Create a .NET Core Application Using Entity Framework Core with Oracle

IdentityServer4 + React + .NET Core + EF Core + MySQL

October 19, 2020 02:03

IdentityServer4 + React + .NET Core + EF Core + MySQL (edit)

https://github.com/tungphuong/Dapper.SimpleCRUD

https://github.com/vietnam-devs/crmcore

https://github.com/vietnam-devs/coolstore-microservices

https://medium.com/hackernoon/clean-domain-driven-design-in-10-minutes-6037a59c8b7b

Executing Raw SQL Queries using Entity Framework Core

February 12, 2020 23:51

The way to executing raw SQL in EF Core (edit)

EF Core Extension class

    public static class EFCoreExt
    {
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public static List<TExecuteQuery<T>(this DBContext dbstring querywhere T : classnew()
        {
            using (var command = db.Database.GetDbConnection().CreateCommand())
            {
                command.CommandText = query;
                command.CommandType = CommandType.Text;
 
                db.Database.OpenConnection();
 
                using (var reader = command.ExecuteReader())
                {
                    var lst = new List<T>();
                    var lstColumns = new T().GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
                    while (reader.Read())
                    {
                        var newObject = new T();
                        for (var i = 0; i < reader.FieldCount; i++)
                        {
                            var name = reader.GetName(i);
                            PropertyInfo prop = lstColumns.FirstOrDefault(a => a.Name.ToLower().Equals(name.ToLower()));
                            if (prop == null)
                            {
                                continue;
                            }
                            var val = reader.IsDBNull(i) ? null : reader[i];
                            prop.SetValue(newObjectvalnull);
                        }
                        lst.Add(newObject);
                    }
 
                    return lst;
                }
            }
        }

EF Core

September 18, 2017 11:15

Getting Started With Entity Framework Core - Console

http://www.learnentityframeworkcore.com/walkthroughs/console-application

Step 1) Creating a .Net Core Console application

Step 2) Creating A Model

Step 3) Adding A Migration
dotnet ef migrations add CreateDatabase
dotnet ef database update

Step 4) Modifying The Database With Migrations
dotnet ef migrations add LimitStrings
dotnet ef database update

How to use the "dotnet ef migrations ..."

<Project Sdk="Microsoft.NET.Sdk.Web">
    ...
        <ItemGroup>
        ...
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />
    </ItemGroup>
    <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
        <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
    ...
</Project>

 

Categories

Recent posts