本文介绍了代码优先 - 数据注释或Fluent API是否更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我喜欢Fluent API,因为我的POCO没有充满EF特定的属性。 p>

有没有什么可以使用Fluent API,你可以使用属性?

解决方案

使用Fluent API,您可以使用DataAnnotations配置的所有内容也是可能的。反之亦然。因此,从配置选项和灵活性的角度来看,Fluent API更好。



Fluent API中可能的配置示例(肯定不是完整列表)但是没有使用DataAnnotations(就我所见):




  • 关闭级联删除:



    .WillCascadeOnDelete(false)


  • 指定外键列名称数据库当关键字没有在您的对象模型中公开时:



    .Map(conf => conf.MapKey(MyForeignKeyID) )


  • 细微调整关系,特别是在对象模型中仅显示关联的一方的所有情况下:



    .WithMany(...) WithOptional(...) code>, WithRequiredDependent(...) WithRequiredPrincipal(...)

    / li>
  • 对象模型和数据库表之间的继承映射规范(Table-Per-Hierarch y,Table-Per-Type,Table-Per-Concrete-Class):



    .Map< TDerived>(Action< EntityMappingConfiguration< TDerived> > ...)




编辑:Microsoft将Fluent API视为高级功能(引自):

但在我看来您非常快速地达到DataAnnotations的限制(除非是非常简单的对象模型)。如果您无法使用DataAnnotations微调您的模型,则最后的手段是遵循默认映射约定(通过根据这些规则命名属性)。目前,您不能覆盖这些约定(只有禁用它们; MS宣布在将来的EF版本中给出约定的配置选项)。但是,如果您不想在定义对象模型时强制映射约定,那么唯一的选择就是Fluent API。



了解Fluent API几乎一个必须的imho,DataAnnotations是一个很好的简单的应用程序。


Just wondering what is a better way to configure my model?

I like the Fluent API, because then my POCO's are not full of EF specific attributes.

Is there anything you can't do with the Fluent API that you can with attributes?

解决方案

Everything what you can configure with DataAnnotations is also possible with the Fluent API. The reverse is not true. So, from the viewpoint of configuration options and flexibility the Fluent API is "better".

Configuration examples (for sure not a full list) which are possible in the Fluent API but not with DataAnnotations (as far as I can see):

  • Switch off cascading deletes:

    .WillCascadeOnDelete(false)

  • Specify foreign key column name in the database when the key isn't exposed in your object model:

    .Map(conf => conf.MapKey("MyForeignKeyID"))

  • Fine granular tuning of relationships, especially in all cases where only one side of an association is exposed in the object model:

    .WithMany(...), WithOptional(...), WithRequiredDependent(...), WithRequiredPrincipal(...)

  • Specification of inheritance mapping between object model and database tables (Table-Per-Hierarchy, Table-Per-Type, Table-Per-Concrete-Class):

    .Map<TDerived>(Action<EntityMappingConfiguration<TDerived>> ...)

Edit: Microsoft considers the Fluent API as an "advanced feature" (Quote from here):

But in my opinion you reach the limitations of DataAnnotations very quickly (except perhaps for extremely simple object models). If you cannot fine tune your model with DataAnnotations anymore your last resort is to follow the default mapping conventions (by naming your properties according to those rules). Currently you cannot overwrite the conventions (only disable them; MS announced to give configuration options for the conventions in future EF releases). But if you don't want to be forced by the mapping conventions when you define your object model, your only option then is the Fluent API.

Learning the Fluent API is almost a Must imho, the DataAnnotations are a nice-to-have for simple applications.

这篇关于代码优先 - 数据注释或Fluent API是否更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:49