Skip to main content

290 posts tagged with ".NET"

.NET Framework and Core for Application Development

View All Tags

How to List EF Core Migrations and See Which Ones Are Applied

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Before applying, reverting or scripting migrations, you need two answers: which migrations exist in the code, and which ones this database already has. dotnet ef migrations list gives both in one list. This guide reads its output, including the (Pending) marker, gets the same list as JSON, lists without a database, and looks at the __EFMigrationsHistory table where EF Core keeps the record.

How to Generate a SQL Script from EF Core Migrations

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

dotnet ef database update applies migrations straight to a database. Often you want the SQL instead: to read exactly what will run, to have a database administrator review it, or to apply it where the tools can't reach. dotnet ef migrations script generates that SQL from your migrations. This guide covers every form of the command with real scripts for MySQL, and shows why reading the SQL catches problems the C# migration hides.

How to Use EF Core Migrations When the DbContext Is in a Class Library

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Keeping the DbContext and its entities in a class library, separate from the web API, is a clean way to structure a .NET C# solution. It also means dotnet ef needs to know about two projects, not one, and the tools' errors when it doesn't are some of the most searched in EF Core. This guide explains the two roles, shows all three errors live, and gives the command that works.

How to Use ConfigureConventions in EF Core to Configure Every Property of a Type

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Some rules apply to every property of a type: all money is decimal(12,2), every DateTime is UTC, every enum is stored by name. Writing those rules property by property is repetitive, and the next property someone adds will miss them. EF Core's ConfigureConventions sets each rule once for the whole model. This guide writes three such rules, proves each one leaves the model unchanged when it replaces per-property code, and shows how a single property can still be different.

How to Configure a One-to-Many Relationship in EF Core

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

One category has many products; each product belongs to one category. That's a one-to-many relationship, the most common shape in any database. EF Core can discover it from your classes alone, and it's worth knowing exactly what it decides for you before you configure anything. This guide models it in C#, shows the MySQL foreign key, index and delete rule EF Core creates by convention, and then configures the same relationship explicitly.

How to Make EF Core Columns Required or Nullable with C# Nullable Reference Types

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Whether a database column may be empty should be decided once, in your C# code, and enforced everywhere: by the compiler, by EF Core and by MySQL. With nullable reference types turned on, EF Core does exactly that: string? becomes a column that allows NULL, string one that doesn't. This guide shows the rule in a real migration, adds C#'s required modifier so the compiler catches missing values, and changes an existing column from nullable to required without breaking.

How to Set a Maximum String Length in EF Core (varchar Instead of longtext in MySQL)

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A C# string has no length limit, so without being told otherwise, EF Core's MySQL provider gives each one the biggest text type MySQL has: longtext, up to 4 GB. That's wasteful for a SKU, and it gets in the way of indexes. This guide sets real maximum lengths so columns become varchar(n), applies the change to a table that already has data, and shows what happens when you index a string without a length.

How to Set Table Names in EF Core with ToTable (Lowercase for MySQL)

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

EF Core names each table after your DbSet property: Products, Categories. On SQL Server or Windows that's harmless, but MySQL on Linux and in Docker compares table names case-sensitively, so products and Products are different tables and hand-written SQL breaks. This guide sets lowercase names with ToTable, shows the rename migration EF Core generates for an existing database, and the SQL MySQL actually runs.

How to Choose What Happens on Delete in EF Core: Cascade or Restrict

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

What should happen to a category's products when the category is deleted? EF Core answers for you, unless you answer first: for a required relationship, its default is to delete the products too, silently. This guide shows that default happening in MySQL, switches it to Restrict so the database refuses instead, and shows the two different errors you get afterwards, one from MySQL and one from EF Core itself.

How to Configure EF Core Entities in Separate Classes with IEntityTypeConfiguration

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Every EF Core rule you add, a table name, a maximum length, an index, a constraint, goes into OnModelCreating by default, and that method grows until nobody wants to touch it. IEntityTypeConfiguration<T> gives each entity its own small class instead. This guide moves a real configuration into such classes, registers them all with one line, and proves the move changed nothing in the database model.