zzzprojects / Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
https://dapper-plus.net/
384 stars 85 forks source link

Mapping, Auto increment #116

Closed AncientD closed 2 years ago

AncientD commented 2 years ago

Description

Hello, please advice how to map object without "Identity", i don’t need that, because my primary key is auto increment, mysql will set it.

Entity<MainContact>()
                .Table("contacts")
                .UseBulkOptions(options =>
                {
                    options.InsertIfNotExists = true;
                })
                .Map(x => x.ClientId, "ClientId")
                .Map(x => x.Value, "Value")
                .Map(x => (int)x.Type, "Type")
                .MapValue(null, "Extension");

Exception

An error occured, no primary key could be found or resolved.

Almost the same if i try this one:

Entity<MainContact>()
                .Table("contacts")
                .UseBulkOptions(options =>
                {
                    options.InsertIfNotExists = true;
                })
                .Identity("Id", true)
                .Map(x => x.ClientId, "ClientId")
                .Map(x => x.Value, "Value")
                .Map(x => (int)x.Type, "Type")
                .MapValue(null, "Extension");

Exception

'Id' is not a member of type 'MainContact' (Parameter 'propertyOrFieldName')

JonathanMagnan commented 2 years ago

Hello @AncientD ,

What's your current key? Is it possible to map it?

You currently using the option InsertIfNotExists, so to make it works, this option needs to be able to know how to check if a row already exists or not but from what I understand, you might currently don't have either this information (so you can only insert but cannot update or delete).

Let me know what I'm currently missing to better understand your scenario.

Best Regards,

Jon

AncientD commented 2 years ago

@JonathanMagnan already fixed, just added id field to my object.

Entity<MainContact>()
                .Table("contacts")
                .UseBulkOptions(options =>
                {
                    options.InsertIfNotExists = true;
                })
                .Identity(x=>x.OutId, "Id")
                .Map(x => x. ClientId, "ClientId")
                .Map(x => x.Value, "Value")
                .Map(x => (int)x.Type, "Type")
                .MapValue(null, "Extension");

But i got another problem, with huge impact for me. i need to insert with exact Id for example i have two rows currently, Primary key 1 Primary key 2

and i need set next id as 5. i have no idea how, trying .Map .Identity .Key Whatever next inserted row will be as 3. and next one is 4.

https://dapper-plus.net/map According to documentation Identity using just as output value for generated by database. .Map should use my value, but it ignoring if i using for PrimaryKey With all other fields - map works fine

image

That rly important for me.

JonathanMagnan commented 2 years ago

Hello @AncientD ,

Here is an online example that show how to do it: https://dotnetfiddle.net/SxF6Eb

In your case, you will probably have to change the Identity for the Key.

Something like this:

Entity<MainContact>()
                .Table("contacts")
                .UseBulkOptions(options =>
                {
                    options.InsertIfNotExists = true;
                    options.InsertKeepIdentity = true;
                })
                .Key(x=>x.OutId)
                .Map(x => x. ClientId, "ClientId")
                .Map(x => x.Value, "Value")
                .Map(x => (int)x.Type, "Type")
                .MapValue(null, "Extension");

Let me know if that worked

AncientD commented 2 years ago

@JonathanMagnan without autoincrement everything work fine, we will remove it from fiend. In our case autoincrement useless