Skip to content

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

Closed
@wolf8196

Description

@wolf8196

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.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions