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

编程入门 行业动态 更新时间:2024-10-28 02:32:46
本文介绍了实体框架:列必须映射,尽管它有一个默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个基于旧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.

更多推荐

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

本文发布于:2023-10-26 22:15:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管它   实体   有一个   默认值   框架

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!