tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
1.79k stars 586 forks source link

Setting custom mapper not working. #286

Closed oslboreal closed 2 years ago

oslboreal commented 2 years ago

I've been trying to use a custom mapper for my domain entities, I have two different projects, one for domain entities called "Domain" and the last one "Infrastructure" in wich I have my repositories.

Infrastructure has his own DependencyInjection class wich is called in my application Startup.cs file. image

I've created a mapper class following the instructions I've found in the documentation. image

Then I have defined my Domain entity, called User. (Inside my domain project) image

Then I use DapperExtensions inside my repository image

But It thrown an exception because the library did'nt find the table "User". So, the library is not applying the configuration I supplied on the startup.

Thank you.

oslboreal commented 2 years ago

Maybe the problem could be that I'm not having POCO classes in the same project I installed DapperExtensions. Is there a way to solve that?

Note: I'm not able to edit the database scheme 👎 I'm dealing with a preexistent database (that really sucks).

Thank you a lot again!

ALMMa commented 2 years ago

Quick questions:

  1. Have you tried to set a custom map for that POCO specifically (eg: ClassMap<User>)?
  2. I'm not sure if Dapper will pick up mappings from the current assembly or not (never even tried, I always have them on a separate assembly). Have you also tried DapperExtensions.DapperExtensions.SetMappingAssemblies([...]);?
oslboreal commented 2 years ago

Yes I tried, but it is still not working. Do you have a repository with your implementation? Maybe I could figure out what's wrong with my code. Anyway I tried A LOT. Thank you!

ALMMa commented 2 years ago

I don't, sorry. I only have internal repos.

For me the structure is as follows:

src/Runner (This is a Desktop executable with Winforms)
src/Web (The web side of the app)
data/Poco
data/Poco.Mappings

On the Web project (MVC) I have, on the ctor of Startup:

var assemblies = new[]
{
   typeof(SomePoco_Mapper).Assembly
};

DapperExtensions.DapperExtensions.SetMappingAssemblies(assemblies);

And on this example, SomePoco_Mapper would be:

public class SomePoco_Mapper : ClassMapper<SomePoco>
{
   //...
}

Nothing else. That also works for desktop. On the Program.cs of the Runner I have the same configuration.

Everything just works there.

Dapper: 2.0.123 DapperExtensions: 1.7.0

valfrid-ly commented 2 years ago

I never used the way you're trying to use.

Did you try to put this in your startup.cs

services.AddAutoMapper(typeof(Startup));

You'll need to add AutoMapper package

PonyoLoveCode commented 2 years ago

Here's how I handled it: Because it has synchronous and asynchronous,so you have to set both. DapperExtensions.DapperExtensions.DefaultMapper = typeof(BetaDapperMapper<>); DapperExtensions.DapperExtensions.SetMappingAssemblies(new List<Assembly>(){typeof(BetaDapperMapper<>).Assembly}); DapperExtensions.DapperAsyncExtensions.DefaultMapper = typeof(BetaDapperMapper<>); DapperExtensions.DapperAsyncExtensions.SetMappingAssemblies(new List<Assembly>() { typeof(BetaDapperMapper<>).Assembly });

I want to use System.ComponentModel.DataAnnotations [table()] [column()] [notmapped()] 企业微信截图_16548410164387

valfrid-ly commented 2 years ago

If you are using both, Sync and Async you may set them both. True.