fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.3k stars 1.59k forks source link

How to Generate ApplicationDbContext Database First #347

Closed cetindogu closed 2 years ago

cetindogu commented 2 years ago

so, is there any easy way to generate entity classes from an existing database

I also

But,

Unhandled exception

System.InvalidOperationException: The entity type 'BLOCK' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943. image

i also need

172 - Support Multiple Key Types for Base Entity #256

https://github.com/fullstackhero/dotnet-webapi-boilerplate/pull/256

cetindogu commented 2 years ago

The Pull Request for "Support Multiple Key Types for Base Entity" approved and merged. But same error on latest code.

iammukeshm commented 2 years ago

@cetindogu what error do you see?

cetindogu commented 2 years ago

On runtime, error is tables "Requires a primary key"

After i set database column name to same as entity class attribute name, no primary key error

And also i cant use directly reverse poco generator(ef core power tools) İ will create a TT file to generate all entity classes, similar ti youra

fretje commented 2 years ago

Your block entity probably doesn't have an Id property that's named according to the convention (the convention is that a field calld Id, or <entityName>Id is regarded as the primary key).

Otherwise, can you show us the definition of the block entity?

cetindogu commented 2 years ago

Primary key problem gone after renamed ID to Id Entity class attribute name and database column names must match

İ need reverse poco generator, possible temproary solutions 1) Via source generators 2) Via T4 file

fretje commented 2 years ago

Isn't that included in ef power tools? A bit off topic anyways for this repo...

cetindogu commented 2 years ago

After generation With ef core power tools, İt needs some other changes 1) our applicationDBcontext .cs must be partial 2) for New context, you have to set the namespaces same With our applicationdbcontext.cs 3) ef core power tools doesnot set the base class So i manually changed the New context inheritance : DbContext inheritance to : IBaseDbContext for compability With our applicationDBcontext.cs 4) ef core power tools entity types doesnot have "Update" method

İ need time to check more and more

fretje commented 2 years ago

I think you should only generate the entities, not the dbcontext... add the dbsets manually to the existing ApplicationDbContext and you're set, right?

The update method is just that... a manually added method on the entity.. you can surely just add that after the entities are generated? This is just one way of mapping dto's to entities btw... there are other ways to do that if you don't want to use an "Update" method...

cetindogu commented 2 years ago

I think you should only generate the entities, not the dbcontext... add the dbsets manually to the existing ApplicationDbContext and you're set, right?

The update method is just that... a manually added method on the entity.. you can surely just add that after the entities are generated? This is just one way of mapping dto's to entities btw... there are other ways to do that if you don't want to use an "Update" method...

Okey i will try Again later. İ didnt want to modify any boilerplate files, to pull latest code changes immediately, so i use partial keyword to change boilerplate files

cetindogu commented 2 years ago

İ got it, just entity generation is enought No need to generate DbContext file