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

ArgumentNullException for DapperPlusEntityMapper<T>.Identity method string overloads #153

Open
wolf8196 opened this issue Jan 7, 2025 · 3 comments
Assignees

Comments

@wolf8196
Copy link

wolf8196 commented Jan 7, 2025

Description

When trying to setup identity auto propagate with string overloads instead of lambdas, I get an exception.
I tried both of these:

public DapperPlusEntityMapper<T> Identity(string name, bool autoPropagateIdentity);
public DapperPlusEntityMapper<T> Identity(string sourceName, string destinationName, bool autoPropagateIdentity);

My assumption from the docs is that "The name", "Name of the source.", "The destination name." are table column/class property that in my case are all the same.
But please let me know if that's not what it means and I'm doing something wrong.

Exception

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source')
   at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.TryGetLast[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
   at Z.Dapper.Plus.DapperPlusAction.IdentityPropagation(BulkOperation bulkOperation, DapperPlusEntityMapper config)
   at Z.Dapper.Plus.DapperPlusAction.Execute()
   at Z.Dapper.Plus.DapperPlusAction..ctor(BaseDapperPlusActionSet action, String key, DapperPlusActionKind kind, Object dataSource)
   at Z.Dapper.Plus.DapperPlusActionSet`1.AddAction(String mapperKey, DapperPlusActionKind actionKind, TEntity item)
   at Z.Dapper.Plus.DapperPlusActionSet`1.DapperPlusActionSetBuilder(DapperPlusContext context, IDbConnection connection, IDbTransaction transaction, String mapperKey, DapperPlusActionKind actionKind, TEntity item, Func`2[] selectors)
   at Z.Dapper.Plus.DapperPlusActionSet`1..ctor(DapperPlusContext context, IDbConnection connection, String mapperKey, DapperPlusActionKind actionKind, TEntity item, Func`2[] selectors)
   at Z.Dapper.Plus.DapperPlusExtensions.BulkInsert[T](IDbConnection connection, String mapperKey, T item, Func`2[] selectors)
   at Z.Dapper.Plus.DapperPlusExtensions.BulkInsert[T](IDbConnection connection, T item, Func`2[] selectors)
   at Program.Main()
Command terminated by signal 6

Fiddle or Project (Optional)

Used your own demo project https://dotnetfiddle.net/nyBt0T#

// Dapper Plus
// Doc: https://dapper-plus.net/identity-key-propagation
// @nuget: Dapper
// @nuget: Microsoft.Data.SqlClient
// @nuget: Z.Dapper.Plus
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.Data.SqlClient;
using Dapper;
using Z.Dapper.Plus;

public class Program
{
	public static void Main()
	{
		// Map your entity
		// DapperPlusManager.Entity<Order>().Identity(x => x.OrderID, true); // works
		DapperPlusManager.Entity<Order>().Identity("OrderID", true); // doesn't work
		// DapperPlusManager.Entity<Order>().Identity("OrderID", "OrderID", true); // doesn't work
		var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
		connection.CreateTable<Order>();
		var newOrders = GetNewOrders();
		FiddleHelper.WriteTable("Orders Before Insert", newOrders);
		connection.BulkInsert(newOrders);
		FiddleHelper.WriteTable("Orders After Insert", newOrders);
	}

	public static List<Order> GetNewOrders()
	{
		var orders = new List<Order>();
		orders.Add(new Order() { State = "New1" });
		orders.Add(new Order() { State = "New2" });
		return orders;
	}

	[Table("Order")]
	public class Order
	{
		[Key]
		[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
		public int OrderID { get; set; }
		public string State { get; set; }
	}
}

Further technical details

Versions are whatever your test projects use.

@JonathanMagnan JonathanMagnan self-assigned this Jan 7, 2025
@JonathanMagnan
Copy link
Member

Hello @wolf8196 ,

Thank you for reporting. We will look into it.

Best Regards,

Jon

@JonathanMagnan
Copy link
Member

Hello @wolf8196 ,

Just to let you know that this issue has been fixed. It will probably be released next Tuesday.

Best Regards,

Jon

@JonathanMagnan
Copy link
Member

JonathanMagnan commented Jan 14, 2025

Hello @wolf8196 ,

The v7.5.7 has been released.

Both methods should have been fixed. The Identity propagation should also propagate when using a string for the property name.

Let me know if everything works with the latest version.

Best Regards,

Jon

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