Vis*_*rma 6 c# types entity-framework entity-framework-5
我使用反射和类型构建器构建动态类,所有,我想知道如何直接类型转换为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));
Run Code Online (Sandbox Code Playgroud)
谢谢
更新 我假设EF现在没有提供实用程序,可以公开访问.接受的答案与Ef无关.那里有不同的图书馆.
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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
将此视为参考
同一个类中还有一个方法static SqlDbType Parse(string stype),它从 sql 类型字符串定义返回枚举 SqlDbType。