本文介绍了[DataType(DataType.EmailAddress)]在流畅的api中有一个计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有错误,Fluent api中 [DataType(DataType.Currency)] 的DataAnnotation的计数器部分是 modelBuilder.Entity< T> .Property(i => i.Price).HasColumnType(Currency)



然后, [DataType(DataType.EmaillAddress)] 的计数器部分是什么?或者是有一个网站,其中列出了如果可以做的数据注释,你可以在流畅的-API。因为我想使用Fluent Api进行验证和映射。谢谢

解决方案

这是一个臭名昭着的混乱区域。



示例:




  • [DataType(DataType.Currency)]注释的属性被实现为 decimal(18,2)(在Sql Server中)。 关闭

  • 流畅的映射 HasColumnType(Money)(而不是货币)创建列为 Money 数据类型。 完美匹配。

  • [DataType(DataType.EmailAddress)] 注释的字符串属性将创建为 nvarchar(max)。这个电邮地址足够了。但是,实际执行特定格式的数据类型的不能靠近



当然,EF可以做得比那更好可以吗那么在后一种情况下,该怎么办?没有内置的电子邮件数据类型,我认为我们不能指望EF用规则和所有操作即时创建用户定义的类型(更不用说Sql Server中的规则已被弃用)。



令人困惑的部分是不同的框架使用不同的数据注释,如所解释的



我不知道EF团队是否通过在代码优先中实现注释的子集做出了正确的决定。当然,它们不能在广泛的System.ComponentModel.DataAnnotations命名空间中实现所有的属性。但目前的实施情况最好是半途而废。上面的例子只是一个小的渗透 - 一个注释被实现,另一个不是。而且,对于这个问题,EF愉快地允许你注释一个 int 属性为 EmailAddress



因此,为了回答你的问题,没有流利的对应的 DataType.EmailAddress 。对于对手来说,没有任何东西。



另一方面,为了说出EF,根本不实现注释将迫使我们多做许多事情。如果我们一起使用MVC和EF,则可以应用一次注释,两个系统都很好地相符。不过,我发现任何来源都没有公开注释和流畅的API之间的完整映射,所以这是一个很麻烦的工作,使注释和流畅的配置相符。



。也许这是最糟糕的一部分:我们必须通过反复试验找出来。任何人在那里启发我们?


If not mistaken, the counter part of DataAnnotation of [DataType(DataType.Currency)] in Fluent api is modelBuilder.Entity<T>.Property(i => i.Price).HasColumnType("Currency").

Then what is the counter part of [DataType(DataType.EmaillAddress)]?

Or is there a site that has a list of if-you-can-do-in-data-annotation-you-can-do-it-in-fluent-api. Because I want to do the validation and mapping using Fluent Api. Thanks

解决方案

This is a notoriously confusing area.

About your examples:

  • A property that is annotated by [DataType(DataType.Currency)] is implemented as decimal(18,2) (in Sql Server). Close.
  • The fluent mapping HasColumnType("Money") (not "Currency") creates the column as Money data type. A perfect match.
  • A string property annotated by [DataType(DataType.EmailAddress)] will be created as nvarchar(max). Granted, that's enough for an email address. But it's nowhere near a data type that enforces a specific format.

Surely EF could do better than that, could it? Well, in the latter case, what should it do? There is no built-in email datatype and I think we can't expect EF to create a user-defined type on the fly with rules and all (not to mention that rules in Sql Server are deprecated).

The confusing part is that data annotations are used differently by different frameworks as is explained here.

I'm not sure whether the EF team made the right decision by implementing a subset of the annotations in code-first. Of course they can't implement all attributes in the extensive System.ComponentModel.DataAnnotations namespace. But the current implementation is half-hearted at best. The examples above are only a small demontration - one annotation is implemented, another isn't. And, for that matter, EF happily allows you to annotate an int property as EmailAddress.

Therefore, to answer your question, there is no fluent counterpart of DataType.EmailAddress. There is nothing to counterpart.

On the other hand, to speak up for EF, not implementing the annotations at all would have forced us to do many things redundantly. If we use MVC and EF together the annotations can be applied once and both systems concur pretty well. It would have been a tedious job to make the annotations and the fluent configurations match.

Unfortunately, I can't find any source disclosing the full mapping between annotations and fluent API. Maybe that's the worst part: we have to find out by trial and error. Anybody out there to enlighten us?

这篇关于[DataType(DataType.EmailAddress)]在流畅的api中有一个计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:48