RepoDB
RepoDB copied to clipboard
Enhancement: TypeMapper for MSSQL (Udt and Spatial)
For User definied Types (Udt) a general TypeMapper for MSSQL would be nice to have.
Example
For Spatial-Types (e.g. SqlGeography) I use dotMorten's Microsoft.SqlServer.Types. With no additional configuration, you can query spatial-data. Doing Updates or Inserts, you have to define somethings for each property:
FluentMapper.Entity<Address>().PropertyValueAttributes(e => e.Geography, new PropertyValueAttribute[]{
new SqlDbTypeAttribute(System.Data.SqlDbType.Udt),
new UdtTypeNameAttribute("Geography")
});
FYI: We will soon release the last 2 months update but this may not be a part of it. This will be a part of the next release after that release.
This is a note, the target fix to this is for the user to be able add the following code.
FluentMapper
.Type<Microsoft.SqlServer.Types.SqlGeography>()
.PropertyValueAttributes(new[]
{
new SqlDbTypeAttribute(SqlDbType.Udt),
new UdtTypeNameAttribute("Geography")
});
EDIT:
Also, via the actual PropertyValueAttributeMapper.
PropertyValueAttributeMapper
.Add<Microsoft.SqlServer.Types.SqlGeography>(e =>
new[]
{
new SqlDbTypeAttribute(SqlDbType.Udt),
new UdtTypeNameAttribute("Geography")
});
Referencing #1059
The fixes to this will be available on the next version > RepoDB v1.12.10.
I tried it, but it doesn't work.
Current call will fail:
FluentMapper
.Type<Microsoft.SqlServer.Types.SqlGeography>()
.PropertyValueAttributes<Microsoft.SqlServer.Types.SqlGeography>(new PropertyValueAttribute[]
{
new SqlDbTypeAttribute(SqlDbType.Udt),
new UdtTypeNameAttribute("Geography")
});
//note, that you need to add the type Microsoft.SqlServer.Types.SqlGeography two times.
The problem can be found in TypeMapFluentDefinition.cs
//this is the current line. It calls itself (infinite loop, stack overflow):
public TypeMapFluentDefinition<TType> PropertyValueAttributes<T>(IEnumerable<PropertyValueAttribute> attributes) =>
PropertyValueAttributes<T>(attributes);
//possible solution, add force-parameter and set it to false
public TypeMapFluentDefinition<TType> PropertyValueAttributes<T>(IEnumerable<PropertyValueAttribute> attributes) =>
PropertyValueAttributes<T>(attributes, false);
//also I think the generic type T isn't necessary
public TypeMapFluentDefinition<TType> PropertyValueAttributes(IEnumerable<PropertyValueAttribute> attributes) =>
PropertyValueAttributes(attributes, false);
public TypeMapFluentDefinition<TType> PropertyValueAttributes(IEnumerable<PropertyValueAttribute> attributes, bool force) =>
PropertyValueAttributes(typeof(TType), attributes, force);
//with these changes, you can write
FluentMapper
.Type<Microsoft.SqlServer.Types.SqlGeography>()
.PropertyValueAttributes(new PropertyValueAttribute[]
{
new SqlDbTypeAttribute(SqlDbType.Udt),
new UdtTypeNameAttribute("Geography")
});
Glad that it is still alpha and not yet beta so this kind of exception can be captured :) You can as well use the PropertyAttributeMapper class explicitly as an alternative from this buggy code.
Hello Mike, I know, this was my last usage. But you can use it, if you add the second parameter force=false. This bypasses the loop-method.
FluentMapper
.Type<Microsoft.SqlServer.Types.SqlGeography>()
.PropertyValueAttributes<Microsoft.SqlServer.Types.SqlGeography>(new PropertyValueAttribute[]
{
new SqlDbTypeAttribute(SqlDbType.Udt),
new UdtTypeNameAttribute("Geography")
}, false);