Add column to table (foreign key) using entity framework

1

I have a C # project on a relatively advanced level.

My DB already contains some data for testing.

I am using entity framework code first migrations

I have the Users and Images tables. The images table has only the Id and ImagePath columns.

I want my Users table to access the Images table so I created a foreign key that is the ImageID (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.ImageImageId". The conflict occurred in database "aspnet-BrainCast-20161008051801", table "dbo.Images", column 'Id'.

There is a conflict even though the table images do not contain any data.

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

    
asked by anonymous 27.02.2017 / 01:36

1 answer

0

Can the image field in the Users table accept null values?

I'm not really 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 to null because it does not yet know what to insert and if the column in the Users table does not accept null it will generate this error.

    
27.02.2017 / 12:54