使用 Linq 将 CLR 对象保存为二进制
是否可以定义自定义类型转换器,以便我可以使用 Linq 将 CLR 对象保存到 sql 字段中?
看起来这个功能应该是可用的。 Visual Studio 2010 Linq-to-Sql 设计器公开每个自动生成的 CLR 属性的 Type 属性。
但是,当我将 type 属性设置为 system.drawing.color 时,出现错误:
Error 1 DBML1005: Mapping between DbType 'Binary(4)'
and Type 'System.Drawing.Color' in Column 'PixelColor' of Type 'Character'
is not supported. 0 0
看来我应该能够以某种方式实现此类型转换器。
Is it possible to define a custom type converter so that I can save CLR objects into a sql field using Linq?
It looks like this capability should be available. The Visual Studio 2010 Linq-to-Sql designer exposes the Type property for each auto-generated CLR property.
However, when I set the type property to system.drawing.color, I get the error:
Error 1 DBML1005: Mapping between DbType 'Binary(4)'
and Type 'System.Drawing.Color' in Column 'PixelColor' of Type 'Character'
is not supported. 0 0
It seems like I should be able to implement this type converter somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Color.ToArgb() 和 FromArgb() 来回转换为可以存储在 dbase 列中的 int 值。如果您确实想要,那么您可以使用 ColorConverter 类来回转换为字符串,您可以将其填充到 PixelColor 列中。不过,使用整数列类型的效率要高得多。
Use Color.ToArgb() and FromArgb() to convert back and forth to an int value that you can store in a dbase column. If you really want to then you can use the ColorConverter class to convert back and forth to a string, something you can stuff into your PixelColor column. Using an integer column type is a heckofalot more efficient though.
谢谢汉斯。
对我来说完整的解决方案是在我的 Linq-to-Sql 类中拥有 2 个属性。一种是 ColorDb,它具有类型 system.Int32 并直接映射到数据库字段。它是由设计师自动创建的。该属性设置为私有。类型为 System.Drawing.Color 的第二个公共属性 ColorClr 执行转换并公开正确键入的数据。
Thanks Hans.
The complete solution for me was to have 2 properties in my Linq-to-Sql class. One is ColorDb which has a type system.Int32 and directly maps to a database field. It is auto-Created by the designer. This property is set to private. A second public property ColorClr which has a type System.Drawing.Color performs the conversion and exposes the data properly typed.