I'm having a problem when I insert a Guid value in an Oracle database (raw(16)
field). I'm using the Devart library in my C# application to record records. The error occurs randomly because I use Guid.NewGuid()
to generate the values that will be recorded in the table.
I was able to simulate through a test project a Guid value that causes the problem. I would like to know if I can create a validation method to assess whether a Guid value generated by Guid.NewGuid
will represent a valid value when recorded in the Oracle database.
Below is the test project used to simulate the problem and the evidence of the invalid value recorded in the database:
Test code:
[TestMethod]
public void RegraNegocio_GerarGuids2()
{
int id;
CanalVenda canalVenda = null;
CanalVenda canalVenda2 = null;
try
{
canalVenda = CanalVenda.New();
canalVenda.RowGuid = Guid.Parse("d8a5feff-51da-4101-9889-77bec5339077");
canalVenda.Nome = $@"Teste Guid {canalVenda.RowGuid.ToString()}";
id = canalVenda.Save().Id;
canalVenda2 = CanalVenda.Get(id);
}
catch (Exception e)
{
throw new Exception($@"Guid: {canalVenda.RowGuid} - Erro: {e.Message}");
}
}
The value recorded in the database: screenshot
Note: Before being written to the database, the value of the Guid variable is converted to a byte array to be compatible with the raw(16)
field.
Guid guidValue = (Guid)value;
value = guidValue.ToByteArray();
In C#, we tried to use the length property to find out if the invalid Guid size was different from a valid Guid, but we were unsuccessful.