Entity Framework: map Guid property in C # to database string

The first application of my EF code should work with SQL Server and Oracle.

  • There is no Guid type in Oracle, so they are stored as nvarchar2(38)

    .
  • There is a GUID type in SQL Server, so no problem.

Is there a way to globally override the Oracle Entity Framework provider behavior so that any columns in the database that mapped Guid types in my POCO classes can have a custom conversion behavior?

Now some might say, "Don't store Guids as strings in Oracle! Use RAW (16) instead, because EF plays nicely with that." Unfortunately this is not an option.

Can I "teach" the Entity framework how to convert string database values ​​to Guides?

Update

I just found this link, which suggests that this feature is not native to EF6, but may be missing in future releases.

Also, I prefer not to have custom converters, displaying private properties as shown here

Refresh

Here's the stack trace from the "Invalid cast from" System.String "exception to" System.Guid "

at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at Oracle.DataAccess.Client.OracleDataReader.ChangeType(Object sourceValue, Type targetType) at Oracle.DataAccess.Client.OracleDataReader.GetValue(Int32 i) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader1.GetUntypedValueDefault(DbDataReader reader, Int32 ordinal) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal) at lambda_method(Closure , Shaper ) at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator1.ReadNextElement(Shaper shaper)

I found a post that Assad contributed to here that says you can change the behavior of System.Convert.ChangeType () which is called by Oracle Data Access Provider

+3


source to share


2 answers


I haven't found a solution. If no other version of the Oracle EF provider exists to support this, the solution is to map the string property that your guid represents to a string in the db. I did it like this:

class Thing { [Required] [StringLength(38)] //38 is the length of a guid with curlies on it public string GuidID { get; set;} }



Then I'll just make sure to do the same ToString () format for hints when storing new values ​​and Guid.Parse () for retrieving values ​​in Chris1804505 mentioned in his answer.

0


source


C # has a built-in GUID class that you might want to consider using this. https://msdn.microsoft.com/en-us/library/system.guid%28v=vs.110%29.aspx

Then when you need to insert the class into the database you can GUID.ToString () and also create the guid using Guid.Parse (GUID). I can lose a little weight, but it should work for you.



As for EF. It's been a while since I've worked with it, but you probably want to change the generated code to work. Or implement a custom class or property to update / change the GUID

-1


source







All Articles