My dapper class mapper I have defined doesn’t appear to be used
I have a class this is defined in a DLL class that I’m trying to keep pure.
public sealed class JointCost : Changeable, ICloneable
{
[Key]
public Guid JointCostId { get; init; } = Guid.NewGuid();
public string JointTypeName { get; set; }
public decimal JointPrice { get;set; }
public JointCostType CostType { get; set; }
public JointType JointType { get; set; }
}
[Flags]
public enum JointType
{
Glue = 1 << 0,
Tape = 1 << 1,
Stitch = 1 << 2,
Other = 1 << 3,
}
public enum JointCostType
{
Per1000LinearFeet,
Per1000SquareInch
}
public enum EChangeable
{
None,
New,
Updated,
Removed
}
public abstract class Changeable
{
private EChangeable _changes = EChangeable.None;
public EChangeable Changes { get => _changes;
set
{
if(value == EChangeable.Removed) {_changes = value; }
else if(value == EChangeable.New) {_changes = value; }
else if(value == EChangeable.Updated) {_changes = value; }
}
}
}
in my webapp project I have a DAL class using dapper where I make a generic database call like so
public async Task<IEnumerable<int>> InsertItemsAsync<T>(IEnumerable<T> items)
where T : Changeable
{
var returnedIds = new List<int>();
using (var connection = new SqlConnection($"{_connectionString} Database={tenantId};"))
{
foreach (var item in items)
{
returnedIds.Add(await connection.InsertAsync(items));
}
}
items.All(x => { x.Changes = EChangeable.None; return false; });
return returnedIds;
}
I wrote a unit test (which is another project) that calls this method like so
[Test]
public async Task GetVendorObject()
{
var newJointCost = new JointCost();
var ids = await _dataContext.InsertItemsAsync(new[] { newJointCost });
}
I get a runtime error “Microsoft.Data.SqlClient.SqlException : Invalid column name ‘Changes’.” So I tried to make a mapping class
so i hadded a mapper class to the webapp project
public class JointCostMapper : ClassMapper<JointCost>
{
public JointCostMapper()
{
Table("JointCosts");
Map(m => m.Changes).Ignore();
AutoMap();
}
}
and in my test method added default mapper and mapping assemblies like so
[Test]
public async Task GetVendorObject()
{
DapperExtensions.DapperExtensions.SetMappingAssemblies(new[] { typeof(JointCostMapper).Assembly });
DapperExtensions.DapperExtensions.DefaultMapper = typeof(JointCostMapper);
DapperExtensions.DapperAsyncExtensions.SetMappingAssemblies(new[] { typeof(JointCostMapper).Assembly });
DapperExtensions.DapperAsyncExtensions.DefaultMapper = typeof(JointCostMapper);
var newJointCost = new JointCost();
var ids = await _dataContext.InsertItemsAsync(new[] { newJointCost });
}
However, after these changes I still get the error that the column ‘Changes’ is invalid. Which is correct as there is not a column ‘changes’, though with the mapper, it should be ignoring the changes property when trying to write.
what am I doing wrong.
I also don’t think I should be setting a default mapper as I’m going to map multiple complex composite objects.