Skip to content

Dapper.EntityFramework package setup #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danjohnso opened this issue Jul 23, 2016 · 4 comments
Closed

Dapper.EntityFramework package setup #570

danjohnso opened this issue Jul 23, 2016 · 4 comments

Comments

@danjohnso
Copy link

Is there something I need to do to use DbGeography types with the entity framework package? I installed the package, but by queries keep returning an empty DbGeography object (all properties are null).

@mgravell
Copy link
Member

Can you indicate what library version (dapper / dapper.ef) and target
framework (.net 4.5.2, .net core 1.0, etc) you are using, and I'll treble
check with an example on that framework.

On 23 Jul 2016 7:22 a.m., "Dan J" [email protected] wrote:

Is there something I need to do to use DbGeography types with the entity
framework package? I installed the package, but by queries keep returning
an empty DbGeography object (all properties are null).


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#570, or mute the
thread
https://github.com/notifications/unsubscribe-auth/AABDsLXIwkYIBuWyAX_aoeoep3jRmhaPks5qYbMzgaJpZM4JTTBM
.

@danjohnso
Copy link
Author

danjohnso commented Jul 23, 2016

1.50.2 dapper, 1.5.0 dapper.ef, and my project is targeting .NET 4.6.1

mgravell pushed a commit that referenced this issue Jul 24, 2016
@mgravell
Copy link
Member

mgravell commented Jul 24, 2016

First, you need to make sure you have the types installed locally - they are not installed by default, and are available from Microsoft here as "Microsoft® System CLR Types for Microsoft® SQL Server® 2012": https://www.microsoft.com/en-gb/download/details.aspx?id=29065 - note that there are separate installers for x86 and x64.

Next thing you need is the EF pieces of Dapper; there was a problem in the System.Runtime reference in 1.50.0, so I've fixed this and deployed as 1.50.2; so you need:

install-package Dapper.EntityFramework

(or via https://www.nuget.org/packages/dapper.entityframework - note that strong-named versions are also available)

Next, you also need an assembly-binding redirect, because MS ships v11, but EF asks for v10. In your app.config / web.config, under <configuration> you can include:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" />
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

And finally, you need to tell dapper "core" about EF:

Dapper.EntityFramework.Handlers.Register();

With that all in place: it should work fine; sorry there are a few steps, but almost all of them are due to MS, not me!

        string redmond = "POINT (122.1215 47.6740)";
        DbGeography point = DbGeography.PointFromText(redmond, DbGeography.DefaultCoordinateSystemId);
        DbGeography orig = point.Buffer(20);


        var fromDb = connection.QuerySingle<DbGeography>("declare @geos table(geo geography); insert @geos(geo) values(@val); select * from @geos",
            new { val = orig });

        Console.WriteLine($"Original area: {orig.Area}");
        Console.WriteLine($"From DB area: {fromDb.Area}");

Let me know how you get on!

@danjohnso
Copy link
Author

Working! I worked around the Runtime by manually adding the facade before I tried installing it, and I already had the types installed with redirect, so it must have been the handler registrations. For some reason I thought that was just magicked into the wrapper. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants