创建ADO.NET映射自定义类型 [英] Creating ADO.NET mapping for custom type

查看:104
本文介绍了创建ADO.NET映射自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我创建的自定义类型,这应该在一个数据库被持久(在此情况下,SQL服务器),为GUID(尽管这个问题是一样有效的,可以被存储为字符串,整数对象, 等等)。

I have a custom type that I created, which should be persisted in a database (in this case, SQL server) as a guid (though this question is just as valid for an object that could be stored as a string, integer, etc).

当我通过这种类型作为参数,以一个DbCommand对象,我得到一个异常:

When I pass this type as a parameter to a DbCommand object, I get an exception:

ArgumentException的:不存在任何映射,从对象类型[...]以一个已知的托管提供原生型

ArgumentException: No mapping exists from object type [...] to a known managed provider native type.

我有CTYPE运营商的类,允许隐式转换为字符串或GUID,但.NET不使用这些,因为它不知道它应该转换的类型。

I have CType operators on the class that allow implicit conversion to a string, or a guid, but .NET doesn't use these since it doesn't know it should convert the type.

有没有一种方法,以一个类型添加到内部映射,或以其他方式告诉.NET如何存储我的自定义类型的数据库?

Is there a way to add a type to the internal mapping, or otherwise tell .NET how to store my custom type in the database?

我明明可以将它传递给.NET的东西时,转换自己的类型,但我想这是自动发生的,像它的大部分内置类型。

I obviously can convert the type myself when passing it to the .NET stuff, but I'd like this to happen automatically, like it does for most of the built-in types.


我试图实现的TypeConverter类我喜欢的类型(自TypeConverter继承,覆盖CanConvertFrom,CanConvertTo,ConvertFrom,的ConvertTo,IsValid的,并添加了System.ComponentModel.TypeConverter属性上我的课),但是这仍然没有按'科技工作。

I've tried to implement a TypeConverter class for my type (inherits from TypeConverter, overrides CanConvertFrom, CanConvertTo, ConvertFrom, ConvertTo, IsValid, and added the System.ComponentModel.TypeConverter attribute to my class), but this still doesn't work.

我猜想它缺了一块,告诉.NET是什么类型的转换存储在数据库中的值时。

I'm assuming it's missing a piece telling .NET what type to convert to when storing the value in the database.


我的解决方法,这一点,现在,是我截取的参数列表,之前我通过他们的SQL服务器,有一个改变我的自定义类型转换为GUID的方法。这是肮脏的,但它确实工作了,但我还是认为这个问题是没有答案。

My workaround for this, for now, is that I intercept the list of parameters, before I pass them to SQL server, and have a method that changes my custom type into a guid. This is dirty, but it does work for now, but I still think this question is unanswered.

推荐答案

据<一href="http://stackoverflow.com/questions/767072/how-should-i-pass-a-user-defined-type-to-sqlparametercollection-addwithvalue">http://stackoverflow.com/questions/767072/how-should-i-pass-a-user-defined-type-to-sqlparametercollection-addwithvalue,这是不可能的。我看了看GetMetaTypeFromValue方法好,它看起来像所有的映射其实很难codeD上的框架,因此没有用户定义的映射是可能的。

According to http://stackoverflow.com/questions/767072/how-should-i-pass-a-user-defined-type-to-sqlparametercollection-addwithvalue, it's not possible. I've looked at the GetMetaTypeFromValue method as well, and it looks like all the mapping is in fact hardcoded in the framework, thus no user-defined mapping is possible.

真讨厌,这将是非常有用的有一个定义映射为一个给定类型的属性。

How annoying, it would be really useful to have an attribute that defines mapping for a given type.

这篇关于创建ADO.NET映射自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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