简体   繁体   English

在LINQ中将枚举值作为存储过程(数据函数)参数传递给SQL

[英]Passing an enum value as a stored procedure (data function) parameter in LINQ to SQL

As mentioned in this question , "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." 该问题中所述 ,“ LINQ to SQL通过指定列的类型允许表映射自动来回转换为Enums-这适用于字符串或整数。” However, I am having trouble with this when using stored procedures (instead of auto-generated SQL) to create and update entities. 但是,在使用存储过程(而不是自动生成的SQL)创建和更新实体时,我遇到了麻烦。

In my database I have a table Users with a column Role of type int. 在我的数据库中,我有一个表Users,该表的列类型为int。 I also have a stored procedure CreateUser which accepts a parameter @role of type int. 我也有一个存储过程CreateUser,它接受int类型的参数@role。 I want user roles to be represented by an enum Role in my C# code. 我希望用户角色在我的C#代码中由枚举角色表示。 I can change the type of the Role property on the autogenerated User class from int to Role, but then the project refuses to compile because the type of the Role property doesn't match the type of the data function role parameter (corresponding to the @role parameter in my stored procedure). 我可以将自动生成的User类上的Role属性的类型从int更改为Role,但是该项目拒绝编译,因为Role属性的类型与数据函数role参数的类型不匹配(对应于@我的存储过程中的角色参数)。 The data function parameter types are autogenerated by LINQ to SQL and I can't find any way to change them. 数据函数参数类型由LINQ to SQL自动生成,我找不到任何更改它们的方法。

I'm considering leaving the Role property as an int, renaming it to something like RoleValue, making it private, and adding another public property of type Role which performs the int-enum conversion. 我正在考虑将Role属性保留为int形式,将其重命名为RoleValue之类,使其私有,并添加另一个类型为Role的公共属性来执行int-enum转换。 But it seems like there ought to be a better way. 但似乎应该有一个更好的方法。

You can overload the CreateUser method.. 您可以重载CreateUser方法。

public Enum Role
{
    Guy = 0,
    Girl = 1
}

public partial LINQtoSQLClass
{
    public ResultObject CreateUser(Role thisRole)
    {
        return CreateUser(Convert.ToInt32(thisRole);
    }
}

It's also common to use T4 Templates to generate the Enumberable class using real-time data from the backend database. 通常,使用T4模板通过后端数据库中的实时数据生成Enumberable类。 That way it matches up with the Ints and you won't have to update it manually. 这样,它就可以与Ints相匹配,而您不必手动更新它。

The data function parameter types are autogenerated by LINQ to SQL and I can't find any way to change them. 数据函数参数类型由LINQ to SQL自动生成,我找不到任何更改它们的方法。

open your DBML file, click on the Role field. 打开您的DBML文件,单击“角色”字段。 the properties window will have a place to specify the server type, as well as the .NET type. 属性窗口将有一个位置来指定服务器类型以及.NET类型。 here's an example of how it works: 这是它如何工作的示例:

Mapping Enum from String 从字符串映射枚举

the answers indicate it works with VARCHARs and TINYINTs so it should work fine with INT as well. 答案表明它可以与VARCHAR和TINYINTs一起使用,因此它也可以与INT一起使用。 the hardest part is probably remembering to put in the right namespace. 最难的部分可能是记住放入正确的名称空间。 good luck! 祝好运!

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

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