为什么实体框架没有按要求注释一些不可为空的列?
我首先使用 EF 4.1 和数据库。
示例表:
CREATE TABLE dbo.Foo(
[ID] [int] IDENTITY(1,1) NOT NULL,
Created datetime not null default(getdate()),
Title varchar(80) not null
PRIMARY KEY CLUSTERED ([ID] ASC)
)
EF 正确加载模型,其中所有 3 列为 nullable = false。
代码生成项“ADO.NET DbContext Generator”的输出:
public partial class Foo
{
public int ID { get; set; }
public System.DateTime Created { get; set; }
public string Title { get; set; }
}
在 MVC3 中,我通过数据库上下文和 foo 模型生成 FooController
。当我打开 /Foo/Create 并在空白表单上点击“创建”时,它在“已创建”字段上显示验证错误,但在“标题”上不显示验证错误。
如果我只输入“创建”日期,则会出现异常:
一个或多个实体的验证失败。请参阅“实体验证错误” 属性了解更多详情
例外情况是“标题字段为必填项”。
我不确定为什么它对一列工作正常,但对另一列则不然。我的第一个修复是简单地添加注释,但是类代码是由 EF 自动生成的。
似乎唯一有效的修复方法是使用部分元数据类: ASP.NET MVC3 - 首先使用 EF 数据库进行数据注释(ObjectConext、DbContext)
我可以根据需要添加 [Required] 标签,但这应该是不必要的。这是 EF 中的错误还是我只是错过了一些东西?
I am using EF 4.1 with database first.
Example table:
CREATE TABLE dbo.Foo(
[ID] [int] IDENTITY(1,1) NOT NULL,
Created datetime not null default(getdate()),
Title varchar(80) not null
PRIMARY KEY CLUSTERED ([ID] ASC)
)
EF correctly loads the model with all 3 columns as nullable = false.
Output from code generation item "ADO.NET DbContext Generator":
public partial class Foo
{
public int ID { get; set; }
public System.DateTime Created { get; set; }
public string Title { get; set; }
}
In MVC3 I generate the FooController
via the db context and foo model. When I bring up /Foo/Create and hit "Create" on the blank form it shows a validation error on "Created" field but not on "Title".
If I enter only a "created" date I get an exception:
Validation failed for one or more entities. See 'EntityValidationErrors'
property for more details
The exception is "The Title field is required".
I'm not sure why it works fine for one column but not the other. My first fix was to simply add the annotation, however the class code is auto generated by EF.
The only fix that seems to work is to use a partial metadata class: ASP.NET MVC3 - Data Annotations with EF Database First (ObjectConext, DbContext)
I can add the [Required] tag as desired however this should be unnecessary. Is this a bug in EF or am I just missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个错误,EF 只是没有添加这些属性。据我所知,数据库优先的方法(由设计者生成的实体类)甚至不执行验证。您引用的链接是解决您问题的有效方法。由于您无法向分部类中的现有属性添加属性,因此引入了包含实际元数据的好友类的原理。
代码优先方法具有验证注释的内置功能,请参阅:Entity Framework 4.1验证。使用数据库优先时的另一个解决方案是创建一个应用这些属性的自定义代码生成器 T4模板和实体框架。
This isn't a bug, EF simply doesn't add those attributes. As far as i know, the database-first approach (Entity classes generated by the designer) doesn't even perform the validation. The link you're refering to is a valid solution for your problem. The principle of buddy-classes which contain the actual metadata was introduced due to the fact, that you cannot add attributes to existing properties in a partial class.
The code-first approach has a built-in functionality to validate your annotations, see: Entity Framework 4.1 Validation. Another solution when using database-first would be to create a custom code-generator that applies those attributes T4 Templates and the Entity Framework.