Skip to content
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

Allow NULL value in MapValue() #111

Closed
VictorioBerra opened this issue Mar 11, 2022 · 6 comments
Closed

Allow NULL value in MapValue() #111

VictorioBerra opened this issue Mar 11, 2022 · 6 comments
Assignees

Comments

@VictorioBerra
Copy link

I have a process that may or may not send a value:

List<CarModel> carsToSync = GetCarsFromAPI();

var maybeElectricMotorType = Console.GetStringFromUserInput();

public class CarModel
{
    public string CarId { get; set; }
    public string ElectricMotorType { get; set; } = null;
    public DateTimeOffset InsertedOnDate { get; set; }
}

DapperPlusManager
    .Entity<CarModel>()
    .Table("Car")
    .Key(x => x.CarId)
    .MapValue(maybeElectricMotorType, nameof(CarModel.ElectricMotorType))
    .AutoMap()

using var connection = new SqlConnection(connectionString);

connection.BulkSynchronize(carsToSync);

If maybeElectricMotorType is null, this will fail with an error from the mapper.

An alternative is to save a reference to DapperPlusEntityTypeMapper:

var dapperPlusEntityTypeMapper = DapperPlusManager
    .Entity<CarModel>()
    .Table("Car")
    .Key(x => x.CarId)
    .AutoMap()

if(maybeElectricMotorType is not null)
{
    dapperPlusEntityTypeMapper.MapValue(maybeElectricMotorType, nameof(CarModel.ElectricMotorType));
}

But I think this could be simpler by allowing nulls, or ignoring the MapValue if the value is null.

@JonathanMagnan JonathanMagnan self-assigned this Mar 11, 2022
@JonathanMagnan
Copy link
Member

Hello @VictorioBerra ,

Thank you for reporting, we will look at it.

I will assign it to my developer to better understand why the null value causes an exception.

Best Regards,

Jon

@JonathanMagnan
Copy link
Member

Hello @VictorioBerra ,

The v4.0.26 has been released.

Let me know if we fixed correctly the issue when the value is null.

Best Regards,

Jon

@VictorioBerra
Copy link
Author

Thanks for the quick turnaround, I appear to have the same issue:

System.Exception: An error occured while resolving the source name mapping of one of your columns.
   at Z.BulkOperations.BulkOperation.()
   at Z.BulkOperations.BulkOperation.Execute()
   at Z.BulkOperations.BulkOperation.BulkSynchronize()
   at Z.Dapper.Plus.DapperPlusAction.Execute()
   at Z.Dapper.Plus.DapperPlusActionSet`1.DapperPlusActionSetBuilder(DapperPlusContext context, IDbConnection connection, IDbTransaction transaction, String mapperKey, DapperPlusActionKind actionKind, IEnumerable`1 items, Func`2[] selectors)
   at Z.Dapper.Plus.DapperPlusActionSet`1..ctor(DapperPlusContext context, IDbTransaction transaction, String mapperKey, DapperPlusActionKind actionKind, IEnumerable`1 items, Func`2[] selectors)
   at Z.Dapper.Plus.DapperPlusExtensions.BulkSynchronize[T](IDbTransaction transaction, String mapperKey, IEnumerable`1 items, Func`2[] selectors)
   at Z.Dapper.Plus.DapperPlusExtensions.BulkSynchronize[T](IDbTransaction transaction, IEnumerable`1 items, Func`2[] selectors)
   at SalesForceDataLoad.Activities.BulkMergeActivity.OnExecuteAsync(ActivityExecutionContext context) in C:\Source\myapp.cs:line 89
   at Elsa.Services.Workflows.WorkflowRunner.TryExecuteActivityAsync(ActivityOperation activityOperation, ActivityExecutionContext activityExecutionContext, IActivity activity, CancellationToken cancellationToken)

It works if I go back to only applying the MapValue when its not null.

Just to clarify a few things, I am using a DateTimeOffset? property (instead of a string from my example), which I am mapping to a nullable DATETIMEOFFSET SQL Server column. I would imagine this does not matter, but I figured id mention it if it helps with testing.

@JonathanMagnan
Copy link
Member

Hello @VictorioBerra ,

Thank you for reporting.

I will ask my developer to try with your column type. We sure indeed fixed something with the null value but perharp that was different of your issue.

@JonathanMagnan
Copy link
Member

Hello @VictorioBerra ,

My developer tried unsuccessfully to reproduce the issue with the latest version

You can check his test online here: https://dotnetfiddle.net/Tzxgaa

Do you think you could create a runnable project with the issue? It doesn’t need to be your project, just a new solution with the minimum code to reproduce the issue. You can send it in private here: info@zzzprojects.com

Best Regards,

Jon

@VictorioBerra
Copy link
Author

@JonathanMagnan Visual Studio did not correctly bump Z.Dapper.Plus to the fixed version, I re-tested and everything seems to be working great! I will close this. Thank you for the fix.

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

No branches or pull requests

2 participants