LINQ to SQL字符串枚举 [英] LINQ to SQL strings to enums

查看:77
本文介绍了LINQ to SQL字符串枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LINQ to SQL通过指定列的类型允许表映射自动来回转换为Enums-这适用于字符串或整数.

LINQ to SQL allows table mappings to automatically convert back and forth to Enums by specifying the type for the column - this works for strings or integers.

有没有一种方法可以使转换不区分大小写,或者在混合中添加自定义映射类或扩展方法,以便我可以更详细地指定字符串的外观.

Is there a way to make the conversion case insensitive or add a custom mapping class or extenstion method into the mix so that I can specify what the string should look like in more detail.

这样做的原因可能是为了在已经设置了数据模式(并且被某些旧有应用程序所依赖)的系统中的一些新的时髦C#代码中提供更好的命名约定.数据库无法更改.

Reasons for doing so might be in order to supply a nicer naming convention inside some new funky C# code in a system where the data schema is already set (and is being relied upon by some legacy apps) so the actual text in the database can't be changed.

推荐答案

您始终可以添加与LinqToSql类同名的局部类,然后定义自己的参数和函数.然后,可以使用这些参数作为该对象的对象参数和方法来访问它们,就像访问自动生成的LinqToSql方法一样.

You can always add a partial class with the same name as your LinqToSql class, and then define your own parameters and functions. These will then be accessible as object parameters and methods for this object, the same way as the auto-generated LinqToSql methods are accessible.

示例:您有一个名为Car的LinqToSql类,该类映射到数据库中的Car表.然后,您可以将文件添加到App_Code中,其中包含以下代码:

Example: You have a LinqToSql class named Car which maps to the Car table in the DB. You can then add a file to App_Code with the following code in it:

public partial class Car {
  // Add properties and methods to extend the functionality of Car
}

我不确定这是否完全满足您更改Enums映射到列的方式的要求.但是,您可以添加一个参数,在该参数中,get/set属性将用于映射所需的枚举,同时保持大小写不敏感.

I am not sure if this totally meets your requirement of changing the way that Enums are mapped into a column. However, you could add a parameter where the get/set properties will work to map the enums that you need while keeping things case-insensitive.

这篇关于LINQ to SQL字符串枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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