首先在EF代码中混合Fluent API和DataAnnotations [英] Mixing Fluent API and DataAnnotations in EF code first

查看:74
本文介绍了首先在EF代码中混合Fluent API和DataAnnotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我们大多数情况下对代码优先的POCO使用流利的配置,但我们发现对表名,PK等内容使用数据批注很有用,因为它使非EF组件更容易使用引用了ObjectContext与这些实体进行交互。

While we mostly use fluent configuration for our code-first POCOs, we have found it useful to use data annotations for things like the table name, PKs, etc. since it makes it easier for non-EF components that don't have a reference to the ObjectContext to interact with these entities.

根据我们的经验,这两种配置样式似乎可以自由地混合使用,而流畅的配置覆盖了DataAnnotations。这在任何地方都有记录吗?

In our experience, it seems that the two configuration styles can be mixed freely, with fluent configuration overriding DataAnnotations. Is this documented anywhere? Is there any risk to doing this mixed configuration?

我们当前正在使用EF 4.3.1

We are currently using EF 4.3.1

推荐答案

我个人没有遇到将代码优先流利的api和数据注释混合在一起的任何问题。我也想知道是否会有交叉的痛苦,我可以说我还没有找到。以下是一些有关此主题的案例研究的参考文献,以使您放心。

I personally haven't ran into any issues with mixing the code first fluent api and data annotations. I also wondered if there would be any crossover pain and I can honestly say I have yet to find any. Here's a few references to case studies on the subject to ease your mind.

(由EF团队直接提供)
http://msdn.microsoft.com/en-us/data/jj591583.aspx

(Direct from the EF team) http://msdn.microsoft.com/en-us/data/jj591583.aspx

(第1部分)
http:/ /www.codeproject.com/Articles/476966/FluentplusAPIplusvsplusDataplusAnnotations-plusWor

这篇关于首先在EF代码中混合Fluent API和DataAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆