-
Notifications
You must be signed in to change notification settings - Fork 109
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
Ambiguous match found. #111
Comments
This happened to me in previos version, when I tried to create a bulk insert of the AuditLogDetails table. |
Show me the code of how u tried the bulk insert |
What is the type of exception ? how to reproduce it ? |
public override int SaveChanges()
{
try
{
var result = 0;
foreach (var ent in ChangeTracker.Entries().Where(c => c.State == EntityState.Added || c.State == EntityState.Modified))
{
using (var auditer = new LogAuditor(ent))
{
var eventType = EventType.Modified;
if (ent.State == EntityState.Added)
{
eventType = EventType.Added;
}
base.SaveChanges();
var record = auditer.GetLogRecord(UserId, eventType, this);
if (record == null) continue;
var detailAuditor = new LogDetailsAuditor(ent, record);
var details = detailAuditor.GetLogDetails().ToList();
if (details.Count <= 0) continue;
Logs.Add(record);
base.SaveChanges();
this.BulkInsert(details);
base.SaveChanges();
}
}
result = base.SaveChanges();
return result;
}
catch (DbEntityValidationException ex)
{
var msg = new StringBuilder();
foreach (var error in ex.EntityValidationErrors.SelectMany(errors => errors.ValidationErrors))
msg.Append($"{error.ErrorMessage}<br/>");
throw new Exception(msg.ToString().Remove(msg.Length - 5, 5));
}
} |
You have made this change to source code ? or overridden the method ? what is this class ? |
I made this change outside the source code, overridden the method, in my DBContext. |
You are adding logs n bulk and calling savechanges method, wouldn't it try to insert logs again ? |
It shouldn't because Log Details is empty before the bulk. |
Same error here but I am doing nothing special. Any insert into the database yields this. I am getting this during Seed() |
Can u send me a small application with minimal code to reproduce this On 18-Jul-2016 11:59 am, "Patrick Michalina" notifications@github.com
|
This started happening on the latest version. I started on a new database and added the new table for metadata. This is thrown on a very basic Seed method. A small class nothing interesting. Both Fluent API and Attribute configuration.
|
To replicate what @patrickmichalina says, make a Master-detail application, something like Country-states-cities. For some reason, when it try to save the log, explote because the multiple insert in log's details. To fix it, I created this extension method: //mLog and mLogsDetails classes have the same structure as an old version of Log and LogDetails
public static void SaveLogs(this DbContext context, List<mLog> logs )
{
foreach (var log in logs)
{
var commandString = new StringBuilder(@"DECLARE @LogId AS BIGINT
INSERT INTO dbo.mLogs ( EventDate , EventType , RecordId , TableName , UserId)");
var lastUnionLength = " UNION ALL".Length + 1;
commandString =
commandString.AppendLine(
$"SELECT '{log.EventDate.ToString("yyyy/MM/dd hh:mm:ss.fff")}',{(byte)log.EventType},{log.RecordId},'{log.TableName}',{log.UserId}");
commandString = commandString.AppendLine("SELECT @LogId = @@IDENTITY");
commandString = commandString.AppendLine("INSERT INTO dbo.mLogsDetails ( LogId , ColumnName , OldValue , NewValue )");
commandString = log.LogDetails
.Aggregate(commandString,
(current, detail) =>
current.AppendLine($"SELECT @LogId, '{detail.ColumnName}', '{(string.IsNullOrEmpty(detail.OldValue) ? "" : detail.OldValue)}', '{(string.IsNullOrEmpty(detail.NewValue) ? "" : detail.NewValue)}' UNION ALL"));
commandString.Remove(commandString.Length - lastUnionLength, lastUnionLength).ToString();
context.Database.ExecuteSqlCommand(commandString.ToString());
}
} And in the SaveChanges method I have this: var logs = new List<mLog>();
foreach (var ent in ChangeTracker.Entries().Where(p => p.State == EntityState.Modified))
{
using (var auditer = new LogAuditor(ent))
{
var state = EventType.Modified;
if (ent.Entity is IBaseEntity)
{
//This is a particulary of the system, so please ignore it
state = (ent.Entity as IBaseEntity).Estado == EstadoRegistro.Eliminado ? EventType.Deleted : EventType.Modified;
}
var record = auditer.GetLogRecord(UserId, state, this);
if (record == null) continue;
logs.Add(record);
}
}
var addedEntries = ChangeTracker.Entries().Where(p => p.State == EntityState.Added).ToList();
var result = base.SaveChanges();
foreach (var ent in addedEntries)
{
using (var auditer = new LogAuditor(ent))
{
var record = auditer.GetLogRecord(UserId, EventType.Added, this);
if (record == null) continue;
logs.Add(record);
}
}
if (logs.Count == 0) return result;
this.SaveLogs(logs);
return result; |
Okay I will try this On Mon, Jul 18, 2016 at 7:44 PM Diomedes Ignacio Domínguez Ureña <
|
Please don gt disgturb mhy email
|
Any updates on this issue? I never had such problems before and may have to revert to the previous version of this package, since all my projects are highly normalized. |
The text was updated successfully, but these errors were encountered: