Error adding models on shared project

Oratile Motaung 0 Reputation points
2023-11-14T13:37:48.4833333+00:00

I have been able to add models on the shared project, but I currently cannot add anymore, I get the following error:

User's image

I did some research and found that the following code might help:

User's image

But instead I got this error:

User's image

-My Microsoft.EntityFrameworkCore,

Microsoft.EntityFrameworkCore.Design,

Microsoft.EntityFrameworkCore.Tools,

Microsoft.EntityFrameworkCoreServer are all the same version 7.0.13

Developer technologies | .NET | Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2023-11-14T13:37:48.49+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Based on the error message provided by the user, it seems like there is an issue with the migration script. The error message indicates that there is a duplicate key value in the database, which means that the migration script is trying to insert a record with a primary key value that already exists in the table.

    To resolve this issue, you can try deleting the migration history table and re-creating the migration script. Here are the steps to do this:

    1. Open the Package Manager Console in Visual Studio.
    2. Run the following command to delete the migration history table:
    Drop-Table __EFMigrationsHistory
    
    1. Delete the existing migration files in the Migrations folder.
    2. Run the following command to create a new migration script:
    Add-Migration InitialCreate
    
    1. Run the following command to update the database:
    Update-Database
    

    This should create a new migration script and update the database with the latest changes. If you still encounter issues, you can try rolling back to a previous migration and then re-applying the latest migration.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.