Add column to a table (foreign key) using Entity framework

Asked

Viewed 330 times

1

I have a project in c# in a relatively advanced phase.

My comic book already contains some test data.

I am using code first Migrations from Entity Framework

I have the Users and Images tables. The images table has only the Id and Path Columns.

I want my Users table to access the Images table so I created a foreign key that is Iddaimagem (primary key of the Images table).

The problem arises when I type the Update-database command, here is the error:

The ALTER TABLE statement conflicted with the FOREIGN KEY Constraint "Fk_dbo.Aspnetusers_dbo.Imagems_imagemid". The Conflict occurred in database "aspnet-Braincast-20161008051801", table "dbo.Imagems", column 'Id'.

There is a conflict even if the table images does not contain any data.

Someone has an idea of how I can solve this. I have been researching but do not understand the solutions that are presented, can also indicate tutorials.

  • can put your most important code, please?

1 answer

0


The image field in the Users table can accept null values?

I’m not quite sure how you structured your classes, but by the message presented, when trying to include the column, as there are already records in the User table, it will set the value as null, because it still does not know what needs to be inserted in it, and if the column in the Users table does not accept null it will generate this error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.