postgresqlentity-framework-coredomain-driven-design.net-8.0clean-architecture

How to make a migration to an existing Postgresql database?


I'm using .NET 8, Visual Studio 2022, Clean Architecture, DDD, EF Core, postgresql.

I have these 4 layers: Infrastructure, Application, Domain, Presentation

I have defined my entities in the domain layer. Let's call them A, B and C.

I have created configurations in the infrastructure layer inheriting from IEntityTypeConfiguration<TEntity>.

I can build and add a migration successfully in my infrastructure layer.

Here is my concern:

I'm connecting to an existing Postgresql database with an existing table that my application is built around to read from. I have defined the content of the existing table as an entity (A) in my domain layer.

How can I create new tables wit my other entities (B and C) while Entity Framework understands that A has an existing table that I only want to read from?

Please ask for further details if I have not expressed myself clearly.

Thank you!

My first migrations seems to want to create a new table

migrationBuilder.CreateTable(
     name: "TransferMessages",
     columns: table => new

How do I point to the existing table so it knows that it is there?


Solution

  • I would go this way:

    1. Create an initial migration only with your Entity A and your DbSet<A>.
    2. Delete all Code which is in the Up() and Down() methods in your initial migration.
    3. Update database with dotnet-ef tool.
    4. Now you have still one Table A in your database, but EF knows how to handle this table.
    5. Introduce Entities B and C with DbSet<B> and DbSet<C>.
    6. Add a Second migration.
    7. Update database with dotnet-ef tool again.

    Now you have 3 Tables in the database and everything should work now.