Do Entity Framework提供将DB类型转换为C#类型的实用程序?
发布时间:2020-12-15 08:43:15 所属栏目:百科 来源:网络整理
导读:我正在使用反射和类型构建器构建动态类,所有,我想知道如何直接类型转换为c#类型 目前我正在做这种方式,但EF提供这种东西吗? case "uniqueidentifier": return typeof(Guid); case "bit": return typeof(Boolean); case "nvarchar": return (typeof(string))
我正在使用反射和类型构建器构建动态类,所有,我想知道如何直接类型转换为c#类型
目前我正在做这种方式,但EF提供这种东西吗? case "uniqueidentifier": return typeof(Guid); case "bit": return typeof(Boolean); case "nvarchar": return (typeof(string)); case "datetime": return typeof(DateTime); case "float": return typeof(double); case "int": return (typeof(int)); 谢谢 UPDATE RyszardD?egan已给出答案,但不能在TT模板或edmx之外使用(或者我可能不知道) 解决方法
我不知道这样的公共方法(我认为没有…它可能被接受为答案)
但是,尝试重新编译sqlmetal.exe,您可以很好地了解内部如何实现默认关联: 在类LinqToSqlShared.Utility.DbTypeSystem(内部类)中,您可以找到以下方法: internal static Type GetClosestRuntimeType(SqlDbType sqlDbType) { switch (sqlDbType) { case SqlDbType.BigInt: return typeof(long); case SqlDbType.Binary: case SqlDbType.Image: case SqlDbType.Timestamp: case SqlDbType.VarBinary: return typeof(Binary); case SqlDbType.Bit: return typeof(bool); case SqlDbType.Char: case SqlDbType.NChar: case SqlDbType.NText: case SqlDbType.NVarChar: case SqlDbType.Text: case SqlDbType.VarChar: return typeof(string); case SqlDbType.DateTime: case SqlDbType.SmallDateTime: case SqlDbType.Date: case SqlDbType.DateTime2: return typeof(DateTime); case SqlDbType.Decimal: case SqlDbType.Money: case SqlDbType.SmallMoney: return typeof(decimal); case SqlDbType.Float: return typeof(double); case SqlDbType.Int: return typeof(int); case SqlDbType.Real: return typeof(float); case SqlDbType.UniqueIdentifier: return typeof(Guid); case SqlDbType.SmallInt: return typeof(short); case SqlDbType.TinyInt: return typeof(byte); case SqlDbType.Xml: return typeof(XElement); case SqlDbType.Time: return typeof(TimeSpan); case SqlDbType.DateTimeOffset: return typeof(DateTimeOffset); } return typeof(object); } 编辑: >见this作为参考>在同一个类中还有一个静态SqlDbType Parse(string stype)方法,它从sql类型字符串定义返回一个枚举SqlDbType. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |