-
-
Notifications
You must be signed in to change notification settings - Fork 317
BulkMerge Not Update the values #827
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
Comments
您发给我的信件已经收到。
我会尽快处理,谢谢!
肖振清
|
Hello @mohammdemara , We are currently missing some information to understand the problem/try to reproduce it. Do you think you could create a runnable project with the issue? It doesn’t need to be your project, just a new solution with the minimum code to reproduce the issue. You can send it in private here: info@zzzprojects.com Best Regards, Jon |
Here Is the Full Code var nationalIds = patientsToMerge.Select(p => p.nationalId).Distinct().ToList();
var requestNumbers = patientsToMerge.SelectMany(p => p.authorizations.Select(z => z.requestNumber)).ToList();
var existingPatients = await _context.Patients
.Where(p => nationalIds.Contains(p.nationalId)).Include(x=>x.authorizations.Where(a=>requestNumbers.Contains(a.requestNumber))).ThenInclude(z=>z.services.Where(s=>s.IsDeleted==false))
.ToListAsync();
List<Domain.Entities.Authorization> updatedAuth = new List<Domain.Entities.Authorization>();
foreach (var syncpatient in patientsToMerge)
{
// Try to find an existing patient by nationalId
var existingParent = existingPatients.FirstOrDefault(p => p.nationalId == syncpatient.nationalId);
if (existingParent != null)
{
// Assign the database generated Id to the in-memory patient
syncpatient.Id = existingParent.Id;
syncpatient.ModifiedDate = DateTime.Now;
// For each authorization in the patient, assign the PatientId and reference
if (syncpatient.authorizations != null)
{
foreach (var auth in syncpatient.authorizations)
{
var existauth = existingParent.authorizations.Where(x => x.requestNumber == auth.requestNumber).FirstOrDefault();
if (existauth != null)
{
auth.PatientID = existingParent.Id;
auth.Patient = existingParent;
auth.ModifiedDate = DateTime.Now;
auth.CreatedDate = existauth.CreatedDate;
auth.Id = existauth.Id;
auth.IsUpdated = false;
updatedAuth.Add(auth);
if (auth.logs != null)
{
foreach (var log in auth.logs)
{
log.AuthorizationId = existauth.Id;
log.Authorization = existauth;
log.CreatedDate = DateTime.Now;
log.Id=Guid.NewGuid();
}
}
if (auth.services.Count > 0)
{
var list2Ids = new HashSet<string>(existauth.services.Select(p => p.serviceCode));
var deletedService = existauth.services
.ExceptBy(auth.services.Select(x => x.serviceCode), x => x.serviceCode)
.ToList();
foreach (var service in auth.services)
{
var existservice= existauth.services.Where(x => x.serviceCode == service.serviceCode).FirstOrDefault();
if (existservice!=null)
{
service.AuthorizationId = existauth.Id;
service.Authorization = existauth;
service.ModifiedDate = DateTime.Now;
service.Id = existservice.Id;
}
else
{
service.AuthorizationId = existauth.Id;
//service.Authorization = existauth;
service.Id = Guid.NewGuid();
}
}
if (deletedService.Count() > 0)
{
deletedService.ForEach(x => x.IsDeleted = true);
auth.services.AddRange(deletedService);
}
}
}
else
{
auth.Id= Guid.NewGuid();
}
}
}
}
else
{
// For new parent, ensure that a new Guid is assigned if needed
if (syncpatient.Id == Guid.Empty)
{
syncpatient.Id = Guid.NewGuid();
syncpatient.CreatedDate = DateTime.Now;
}
// The children (Authorizations) will reference the new patient record
if (syncpatient.authorizations != null)
{
foreach (var auth in syncpatient.authorizations)
{
auth.PatientID = syncpatient.Id;
auth.Patient = syncpatient;
}
}
}
}
using (var transaction = _context.Database.BeginTransaction())
{
try
{
await _context.BulkMergeAsync(patientsToMerge, options =>
{
options.IncludeGraph = true;
//options.UpdateMatchedAndConditionExpression = x => new { x.ModifiedDate };
options.IncludeGraphOperationBuilder = operation =>
{
switch (operation)
{
case BulkOperation<Patient> patient:
//patient.InsertIfNotExists = true;
patient.ColumnPrimaryKeyExpression = x => x.nationalId;
patient.AutoMapOutputDirection = true;
patient.AllowDuplicateKeys = true;
break;
case BulkOperation<Domain.Entities.Authorization> authorization:
// authorization.InsertIfNotExists = true;
authorization.ColumnPrimaryKeyExpression = x => x.requestNumber;
authorization.AutoMapOutputDirection = true;
authorization.AllowDuplicateKeys = true;
break;
case BulkOperation<Service> service:
// service.InsertIfNotExists = true;
service.ColumnPrimaryKeyExpression = x => x.Id;
service.AutoMapOutputDirection = true;
service.AllowDuplicateKeys = true;
break;
case BulkOperation<Domain.Entities.Log> log:
// log.InsertIfNotExists = true;
log.ColumnPrimaryKeyExpression = x => x.Id;
log.AutoMapOutputDirection = true;
log.AllowDuplicateKeys = true;
// log.
break;
}
};
});
transaction.Commit();
}
public class Patient :Entity
{
public required string nationalId { get; set; }
public required DateOnly dateofBirth { get; set; }
public required string patientName { get; set; }
public required string mobileNumber { get; set; }
public required List<Authorization> authorizations { get; set; }
}
public class Log :Entity
{
public Guid AuthorizationId { get; set; }
public int revisionNumber { get; set; }
public required string status { get; set; }
public required string statusCode { get; set; }
public required Authorization Authorization { get; set; }
}
public class Authorization:Entity
{
public Guid PatientID { get; set; }
public required string requestNumber { get; set; }
public required string approvalStatus { get; set; }
public required Patient Patient { get; set; }
public required List<Log> logs { get; set; }
public required List<Service> services { get; set; }
}
public class Service:Entity
{
public required string serviceCode { get; set; }
[StringLength(int.MaxValue, MinimumLength = 3)]
public required string serviceDescription { get; set; }
public Guid AuthorizationId { get; set; }
public required Authorization Authorization { get; set; }
} |
Hello @mohammdemara, Thank you for the code. I’m not sure if it will be enough for us to fully reproduce the issue, but my developer will give it a try. Best regards, Jon |
Hello @mohammdemara , After trying it, my developer will really need a runnable project with the issue. So he could work fixing this issue instead of trying to reproduce it. As said, It doesn’t need to be your project, just a new solution with the minimum code to reproduce the issue. You can send it in private here: info@zzzprojects.com Best Regards, Jon |
Hello @mohammdemara Since our last conversation, we haven't heard from you. As previously mentioned, we would need a runnable project to assist you. Best regards, Jon |
I have One Entity have 3 child and using bulk merge function it works fine if the record new but if on the row is updated not updating the values on db below is the code sample . Please guide what is missed here or what is the issue on this code .
The text was updated successfully, but these errors were encountered: