Dapper-FluentMap
Dapper-FluentMap copied to clipboard
Invalid column name when executing a command (insert or update) a second time in another entity
The exception occurs in the following cases: First: I execute one of the Add or Update commands by passing an entity, for example Customers. and then execute any of the commands on another entity, such as Orders.
No matter the order of execution, the message that appears is invalid column on the second execution.
It seems that the problem is in mapping the base class (Multi Mapping Id) that is in memory generating the exception:
Map (p => p.Id) .IsKey (). ToColumn ("IdActType");
I solved the problem by creating the property in the class and ignoring the base class Id:
public class ActTypeMap: DommelEntityMap <ActType> { public ActTypeMap () { ToTable ("ActType"); Map (p => p.Id) .Ignore(); Map (p => p.IdTypeAto) .IsKey (); } }
public abstract class Entity : IEquatable<Entity> { public Entity() { Id = Guid.NewGuid(); Timestamp = DateTime.Now; }
public Guid Id { get; set; }
public Guid IdUser { get; set; }
public DateTime Timestamp { get; set; }
public bool Equals(Entity other) => Id == other.Id;
}
public abstract class DapperGenericRepository<TEntity> : IRepository<TEntity> where TEntity : Entity { protected readonly string ConnectionString;
public DapperGenericRepository()
{
if (FluentMapper.EntityMaps.IsEmpty)
{
RegisterMappings.Register();
}
var config = new ConfigurationBuilder()
.AddJsonFile("appSettings.json")
.Build();
ConnectionString = config.GetConnectionString("DefaultConnection");
}
public virtual void Add(TEntity entity)
{
entity.Timestamp = DateTime.Now;
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
conn.Insert(entity);
}
}
public virtual void Update(TEntity entity)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
conn.Update(entity);
}
}
}
public static class RegisterMappings { public static void Register() { FluentMapper.Initialize(c => { c.AddMap(new CustomerMap()); c.AddMap(new ActTypeMap());
c.ForDommel();
});
}
}