Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework: Column must be mapped although it does have a default value?

I am trying to create an Entity Framework 4 model based on a legacy SQL server 2008 database. Since the model is going to be published in a web service, I need to omit an integer column called NewsletterSubscribed from one of the model's entities.

After removing the column in the EF designer I got the following compilation error:

Error   59  Error 3023: Problem in mapping fragments starting at line 356:Column Users.NewsletterSubscribed in table Users must be mapped: It has no default value and is not nullable.
    C:\Users\Adrian\Documents\Sites\Zeiterfassung\Zeiterfassung\API\V1\EFModel.edmx 357 15  Zeiterfassung

But the column seems to have a default value of 0 bound to it. I tried running this SQL statement against the database:

ALTER TABLE [dbo].[Users] ADD  DEFAULT ((0)) FOR [NewsletterSubscribed]

But that also fails:

Msg 1781, Level 16, State 1, Line 3 Column already has a DEFAULT bound to it. Msg 1750, Level 16, State 0, Line 3 Could not create constraint. See previous errors.

So either the column does not have a default value (in which case I am not sure why I can't create one), or Entity Framework doesn't see it. What is going on?

Thanks,

Adrian

like image 606
Adrian Grigore Avatar asked Jun 19 '10 17:06

Adrian Grigore


1 Answers

Open the .edmx with Visual Studio XML editor rather than with the Entity Designer, and add a DefaultValue="0" attribute to the unmapped column in the SSDL. For some reasons these are not generated from the database.

like image 102
Julien Lebosquain Avatar answered Nov 16 '22 03:11

Julien Lebosquain