简体   繁体   English

如何首先使用实体​​CTP4模型,然后添加数据注释(例如验证规则)?

[英]How do I use Entities CTP4 model first and then add data annotations such as validation rules?

I'm creating a model, which is then generating SQL to create the database. 我正在创建一个模型,然后该模型将生成SQL以创建数据库。 Now I have some great entity classes in a single .designer.cs file. 现在,我在单个.designer.cs文件中有一些很棒的实体类。

However, I then want to add [Required] to some of the fields that I've created model-first. 但是,然后我想将[Required]添加到我首先创建的某些字段中。 I've created public partial classes, but I can't redefine the fields to add the [Required] annotation. 我已经创建了公共局部类,但是我无法重新定义字段以添加[Required]注释。

Any thoughts? 有什么想法吗?

As far as I can tell so far, with CTP5, this hasn't really changed from plain EF 4; 就目前为止我所知,对于CTP5,这与普通的EF 4并没有真正改变。 you create partial classes for the entities you need to validate, then use the MetadataTypeAttribute. 您为需要验证的实体创建部分类,然后使用MetadataTypeAttribute。 This is an obnoxious way to do things, but you can read all about it on MSDN here . 这是做事的讨厌方式,但是您可以在MSDN上阅读有关它的全部信息。

CTP 5 also added a T4 generation template that lets you use the model to generate DbContext based classes instead of the more traditional ones from plain EF 4. This is detailed here. CTP 5还添加了一个T4生成模板,使您可以使用该模型生成基于DbContext的类,而不是普通EF 4中更传统的类 . This doesn't change, as far as I can see, the need to use partials and MetadataTypeAttributes. 据我所知,使用partials和MetadataTypeAttributes的需求并没有改变。

I'm still hoping for some convergence here, but so far most of the real goodies in the CTPs seem to be going to the Code-First camp which can now use data annotations without any acrobatics. 我仍然希望在这里有所融合,但是到目前为止,CTP中大多数真正的好东西似乎都将进入Code-First阵营,该阵营现在可以使用数据注释而无需任何杂技。 Of course, finding solid info on the CTPs is also a bit hard. 当然,要找到有关CTP的可靠信息也有些困难。 The docs there, and the web is polluted with so much noise from previous CTPs that good info is getting very hard to find. 那里的文档以及以前的CTP发出的大量噪音污染了网络,以至于很难找到好的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM