简体   繁体   English

如何在Entity Framework Core中正确处理SQL_VARIANT?

[英]How do you properly handle SQL_VARIANT in Entity Framework Core?

It looks like support has recently been added to Entity Framework Core in .NET Core 2.1 (preview) to allow the mapping of SQL_VARIANT columns ( https://github.com/aspnet/EntityFrameworkCore/issues/7043 ). 看起来最近在.NET Core 2.1 (preview)向Entity Framework Core添加了支持,以允许映射SQL_VARIANT列( https://github.com/aspnet/EntityFrameworkCore/issues/7043 )。

It looks like the way to go about doing this is using the new HasConversion() method ( https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions ). 看起来这样做的方法是使用新的HasConversion()方法( https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions )。

So, in order to map my SQL_VARIANT column and treat the underlying datatype as being a VARCHAR of any length (I only care about reading it at this point), I can do the following (where the Value property here is of type object in the model): 因此,为了映射我的SQL_VARIANT列并将基础数据类型视为任何长度的VARCHAR (我只关心此时读取它),我可以执行以下操作(其中Value属性的类型为object模型):

entity.Property(e => e.Value).HasConversion(v => v.ToString(),
                                                            v => v.ToString());

This works, if the SQL_VARIANT 's underlying datatype is a VARCHAR of any length. 如果SQL_VARIANT的基础数据类型是任何长度的VARCHAR ,则此方法有效。

However, being a SQL_VARIANT , the column could contain data of other types, such as DATETIME values. 但是,作为SQL_VARIANT ,该列可以包含其他类型的数据,例如DATETIME值。

For simplicity I've only specified DateTime and string here, but in theory I'd probably want to support the datatypes necessary to map whatever could be stored in a SQL_VARIANT column, if possible. 为简单起见,我在这里只指定了DateTimestring ,但理论上我可能希望支持映射SQL_VARIANT列中存储的所有数据类型,如果可能的话。

How would I go about determining which one of those two types ( string and DateTime ) I'd want to map to at runtime? 我将如何确定在运行时我想要映射到的这两种类型中的哪一种( stringDateTime )? Is there a way to do this? 有没有办法做到这一点?

As I understand, the way to go at the moment is just: 据我了解,目前的方法是:

// where e.Value is property of type "object"
entity.Property(e => e.Value).HasColumnType("sql_variant");

And that's all, no need for any custom converters. 这就是全部,不需要任何自定义转换器。 As pull message adding this feature states: 添加此功能的消息说明:

The type mapper will now map properties to sql_variant columns if: 现在,类型映射器将属性映射到sql_variant列,如果:

  • The property is type object 该属性是类型对象
  • The store type name is specified as sql_variant 商店类型名称指定为sql_variant

You current code satisfies first condition (property is of type object ), but does not satisfy second one. 您当前的代码满足第一个条件(属性是object类型),但不满足第二个条件。 Why exactly it cannot infer store type name from property being of type object - I'm not really sure, maybe just because Entity Framework is not sql-server specific and supports many other databases, in which object property can have different semantics or not supported at all, so it requires to state your intentions explicitly. 为什么它不能从属性类型对象的属性推断存储类型名称 - 我不太确定,可能只是因为实体框架不是特定于sql-server并且支持许多其他数据库,其中object属性可以具有不同的语义或不支持所以,它需要明确说明你的意图。

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

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