RepoDB icon indicating copy to clipboard operation
RepoDB copied to clipboard

Enhancement: TypeMapper for MSSQL (Udt and Spatial)

Open Garios opened this issue 3 years ago • 1 comments

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")
        });

Garios avatar Jan 21 '22 11:01 Garios

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.

mikependon avatar Jan 28 '22 06:01 mikependon

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")
        });

mikependon avatar Aug 16 '22 08:08 mikependon

Referencing #1059

mikependon avatar Aug 18 '22 06:08 mikependon

The fixes to this will be available on the next version > RepoDB v1.12.10.

mikependon avatar Sep 07 '22 04:09 mikependon

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")
	});

Garios avatar Oct 14 '22 09:10 Garios

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.

mikependon avatar Oct 14 '22 10:10 mikependon

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);

Garios avatar Oct 14 '22 10:10 Garios