我试图用BlToolkit将一个实体插入到oracle数据库中。连接是与System.Data.OracleClient。mssql服务器一切都很好。但是,当我迁移到 oracle 时,插入 guid 会给我错误。选择完美。有什么建议吗?
型:
[TableName("TEST")]
public class Test
{
[MapField("TEST_ID")]
[PrimaryKey]
public Guid TestId { get; set; }
[DisplayName("Kodu")]
[MapField("TEST_KODU")]
public string TestKodu { get; set; }
[DisplayName("Test Kısa Adı")]
[MapField("TEST_KISA_ADI")]
public string TestKisaAd { get; set; }
[DisplayName("Test Adı")]
[MapField("TEST_ADI")]
public string TestAdi { get; set; }
public Test()
{
}
public Test(Guid _id, string _kod, string _adi, string _kisaAdi)
{
TestId = _id;
TestKodu = _kod;
TestKisaAd = _kisaAdi;
TestAdi = _adi;
}
}
数据库管理器:
public class DbProvider : DbManager
{
public DbProvider() : base("ElabConnectionString")
{
}
}
在行动:
var db = new DbProvider();
db.Insert(test);
给定错误:
Hata: BLToolkit.Data.DataException: Failed to convert parameter value from a Guid to a Byte[]. ---> System.InvalidCastException: Failed to convert parameter value from a Guid to a Byte[]. ---> System.InvalidCastException: Object must implement IConvertible.
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Data.OracleClient.OracleParameter.CoerceValue(Object value, MetaType destinationType)
--- End of inner exception stack trace ---
at System.Data.OracleClient.OracleParameter.CoerceValue(Object value, MetaType destinationType)
at System.Data.OracleClient.OracleParameter.SetCoercedValueInternal(Object value, MetaType metaType)
at System.Data.OracleClient.OracleParameterBinding.PrepareForBind(OracleConnection connection, Int32& offset)
at System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle, CommandBehavior behavior, Boolean needRowid, OciRowidDescriptor& rowidDescriptor, ArrayList& resultParameterOrdinals)
at System.Data.OracleClient.OracleCommand.ExecuteNonQueryInternal(Boolean needRowid, OciRowidDescriptor& rowidDescriptor)
at System.Data.OracleClient.OracleCommand.ExecuteNonQuery()
at BLToolkit.Data.DbManager.ExecuteOperation[T](OperationType operationType, Func`1 operation)
--- End of inner exception stack trace ---
at BLToolkit.Data.DbManager.OnOperationException(OperationType op, DataException ex)
at BLToolkit.Data.DbManager.HandleOperationException(OperationType op, Exception ex)
at BLToolkit.Data.DbManager.ExecuteOperation[T](OperationType operationType, Func`1 operation)
at BLToolkit.Data.DbManager.ExecuteNonQueryInternal()
at BLToolkit.Data.DbManager.ExecuteNonQuery()
at BLToolkit.Data.DbManager.BLToolkit.Data.Linq.IDataContext.ExecuteNonQuery(Object query)
at BLToolkit.Data.Linq.Query`1.NonQueryQuery(IDataContextInfo dataContextInfo, Expression expr, Object[] parameters)
at BLToolkit.Data.Linq.Query`1.<SetNonQueryQuery>b__e(QueryContext ctx, IDataContextInfo db, Expression expr, Object[] ps)
at BLToolkit.Data.Linq.Query`1.Insert(IDataContextInfo dataContextInfo, T obj)
at BLToolkit.Data.Linq.Extensions.Insert[T](IDataContext dataContext, T obj)
Oracle 中没有 Guid (UniqueIdentifier) 数据类型,您必须提供一个字节数组。在 Oracle 中,将 Guid 定义为 RAW(16),然后在 C# 程序中,使用 Guid.ToByteArray() 方法。
BLToolkit 支持 GUID for Oracle 作为 RAW(16):
https://github.com/igor-tkachev/bltoolkit/blob/b9dae9888047db5df4f5cbc6a705aabfc629712c/Data/Create%20Scripts/Oracle.sql#L504
以及如何使用它:
https://github.com/igor-tkachev/bltoolkit/blob/5107745c15f6f04271f0a2140ab96c62054834fa/UnitTests/Linq/Types.cs#L126