实体框架:列必须映射,尽管它有一个默认值?

实体框架:列必须映射,尽管它有一个默认值?

问题描述:

我正在尝试创建一个基于旧SQL Server 2008数据库的实体框架4模型。由于该模型将在Web服务中发布,因此我需要从模型的一个实体中省略一个名为NewsletterSubscribed的整数列。

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.

删除EF设计器中的列后,我收到以下编译错误:

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

但是列似乎默认值为0。我尝试对数据库运行这个SQL语句:

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]

但这也失败了:


消息1781,级别16,状态1,行3
列已经有一个DEFAULT绑定到
它。消息1750,级别16,状态0,行
3无法创建约束。请参阅
以前的错误。

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?

谢谢,

Adrian

使用Visual Studio XML编辑器而不是实体设计器打开.edmx,并添加一个 DefaultValue =0属性到SSDL中的未映射列。由于某些原因,这些不是从数据库生成的。

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.