CodeFirstDbGenerator 1.1.0
See the version list below for details.
dotnet add package CodeFirstDbGenerator --version 1.1.0
NuGet\Install-Package CodeFirstDbGenerator -Version 1.1.0
<PackageReference Include="CodeFirstDbGenerator" Version="1.1.0" />
paket add CodeFirstDbGenerator --version 1.1.0
#r "nuget: CodeFirstDbGenerator, 1.1.0"
// Install CodeFirstDbGenerator as a Cake Addin #addin nuget:?package=CodeFirstDbGenerator&version=1.1.0 // Install CodeFirstDbGenerator as a Cake Tool #tool nuget:?package=CodeFirstDbGenerator&version=1.1.0
CodeFirstDbGenerator
Motivation: allow to create migrations and update database without installing Entity Framework, for libraries like Dapper.
Installation
Db Supported:
- Sql Server
Install packages : CodeFirstDbGenerator, CodeFirstDbGenerator.SqlServer, CodeFirstDbGenerator.Tools
install-package CodeFirstDbGenerator
install-package CodeFirstDbGenerator.SqlServer
dotnet tool install --global CodeFirstDbGenerator.Tools
Recommendation : Create a class Library .NET 5 or .NET 6 for Migrations
Create a migration
Use the tool to generate migrations
dotnet cf add-migration InitialCreate -p path/to/project
Define the entites to create/update the database. CF will discover columns, primary keys and foreign keys with Data Annotations
[Migration("637727087400479557_InitialCreate")]
public class InitialCreate : Migration
{
protected override MigrationOptions GetOptions()
{
return new SqlServerMigrationOptions();
}
protected override MigrationRunner GetMigrationRunner()
{
var runner = new SqlServerMigrationRunner("Server=(localdb)\\mssqllocaldb;Database=TestMG;Trusted_Connection=True;MultipleActiveResultSets=true");
runner.DropDatabase = true; // allows to drop existing database
return runner;
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable<Company>();
migrationBuilder.CreateTable<Employee>();
// a stored procedure
migrationBuilder.Sql(@"
CREATE PROC usp_GetCompany
@CompanyId int
AS
BEGIN
SELECT *
FROM Companies
WHERE CompanyId = @CompanyId
END
GO
");
}
}
// [Table("tbl_Companies")] allows to define the name of the table
public class Company
{
[Key]
public int CompanyId { get; set; } // key
[StringLength(100)]
public string Name { get; set; } //required
[Required]
//[Column("MyPostalCode")] allows to rename the column
public string PostalCode { get; set; } // required with data annotations
public string? Address { get; set; }
[MaxLength(50)] // or [StringLength(50)]
public string? City { get; set; } // not required
[Timestamp]
public byte[] RowVersion { get; set; }
public List<Employee> Employees { get; set; } = new(); // ignored
}
public class Employee
{
[Key]
public int EmployeeId { get; set; } // recommendation: make Key unique, dont use names like "Id" for all primary keys
public string FirstName { get; set; }
public string LastName { get; set; }
// [ForeignKey("Companies")] use foreign key attribute if column name does not match with principal column name
public int CompanyId { get; set; }
public Company Company { get; set; } // ignored
[NotMapped] // ignored
public string FullName
{
get { return $"{FirstName} {LastName}"; }
}
}
DataAnnotations Attributes:
- Key: for primary key (identity if int, short or long)
- Table: to define table name
- Column: to define column name
- DatabaseGenerated + identity option: for a column identity
- StringLength ou MaxLength: to define string length (exeample "navarchar(100)")
- ForeignKey: to specify the principal table name
Its possible to define columns (default, value, unique, column type, etc.), multipe primary keys or foreign keys with fluent api. Example:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable<Company>();
migrationBuilder
.CreateTable<Employee>()
.Column(x => x.FirstName, defaultValue: "X")
.ForeignKey(column: x => x.CompanyId,
principalTable: "Companies",
principalColumn: "CompanyId",
onUpdate: ReferentialAction.Cascade,
onDelete: ReferentialAction.Cascade);
migrationBuilder.CreateTable<Student>().PrimaryKey(columns: x => new { x.StudentKey, x.AdmissionNum });
}
Migrations History:
By default a table "__CFMigrationsHistory" is created. Its possible to create a custom MigrationsHistoryManager. Just implement IMigrationsHistoryManager
and change the manager provided
protected override MigrationRunner GetMigrationRunner()
{
var runner = new SqlServerMigrationRunner("Server=(localdb)\\mssqllocaldb;Database=TestMG;Trusted_Connection=True;MultipleActiveResultSets=true"
, new MyMigrationsHistoryManager());
return runner;
}
Update database
With tool
dotnet cf update-database -p path/to/library.dll
Or in code
MigrationRunner.UpdateDatabase();
// or with path
MigrationRunner.UpdateDatabase("path/to/library.dll");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on CodeFirstDbGenerator:
Package | Downloads |
---|---|
CodeFirstDbGenerator.SqlServer
A Lightweight Library to generate quickly Databases from entities. Concepts: - 0 Migration. No "MigrationsHistory" table. Avoids a directory with hundred of migrations. - Scaffold command to generate/synchronize entities, DbModel and Snapshot with Database - Generate script or update database command to update the database Db Supported: SQL Server, SQLite, MySQL|MariaDB, PostgreSQL |
|
CodeFirstDbGenerator.Sqlite
A Lightweight Library to generate quickly Databases from entities. Concepts: - 0 Migration. No "MigrationsHistory" table. Avoids a directory with hundred of migrations. - Scaffold command to generate/synchronize entities, DbModel and Snapshot with Database - Generate script or update database command to update the database Db Supported: SQL Server, SQLite, MySQL|MariaDB, PostgreSQL |
|
CodeFirstDbGenerator.MySql
A Lightweight Library to generate quickly Databases from entities. Concepts: - 0 Migration. No "MigrationsHistory" table. Avoids a directory with hundred of migrations. - Scaffold command to generate/synchronize entities, DbModel and Snapshot with Database - Generate script or update database command to update the database Db Supported: SQL Server, SQLite, MySQL|MariaDB, PostgreSQL |
|
CodeFirstDbGenerator.Npgsql
A Lightweight Library to generate quickly Databases from entities. Concepts: - 0 Migration. No "MigrationsHistory" table. Avoids a directory with hundred of migrations. - Scaffold command to generate/synchronize entities, DbModel and Snapshot with Database - Generate script or update database command to update the database Db Supported: SQL Server, SQLite, MySQL|MariaDB, PostgreSQL |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.0.0 | 1,178 | 7/30/2022 |
5.8.1 | 1,150 | 4/6/2022 |
5.8.0 | 1,130 | 4/5/2022 |
5.7.2 | 1,063 | 4/3/2022 |
5.7.0 | 1,141 | 4/2/2022 |
5.2.5 | 945 | 3/17/2022 |
4.0.3 | 976 | 1/13/2022 |
4.0.2 | 464 | 1/11/2022 |
4.0.1.1 | 288 | 12/28/2021 |
4.0.0.1 | 345 | 12/28/2021 |
3.2.0 | 532 | 12/19/2021 |
3.1.0.3 | 303 | 12/18/2021 |
3.1.0 | 291 | 12/18/2021 |
3.0.1.6 | 562 | 12/17/2021 |
2.0.8 | 419 | 12/7/2021 |
1.2.0.2 | 2,083 | 11/26/2021 |
1.2.0.1 | 389 | 11/22/2021 |
1.1.1 | 1,198 | 11/20/2021 |
1.1.0 | 824 | 11/19/2021 |
1.0.1.1 | 438 | 11/18/2021 |