fixup new service resolution context
This commit is contained in:
parent
1e6b239350
commit
c935322bc0
|
|
@ -21,6 +21,8 @@ namespace NejCommon.Models;
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract CommonDbContext CreateCopy();
|
||||||
|
|
||||||
public async Task<bool> ApiSaveChangesAsync()
|
public async Task<bool> ApiSaveChangesAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -78,19 +80,22 @@ namespace NejCommon.Models;
|
||||||
{
|
{
|
||||||
var entity = ChangeTracker.Entries<T>().Select(x => x.Entity).FirstOrDefault(predicate.Compile());
|
var entity = ChangeTracker.Entries<T>().Select(x => x.Entity).FirstOrDefault(predicate.Compile());
|
||||||
|
|
||||||
|
//find in change tracker
|
||||||
if (entity != null && Entry(entity).State == EntityState.Deleted)
|
if (entity != null && Entry(entity).State == EntityState.Deleted)
|
||||||
{
|
{
|
||||||
Entry(entity).State = EntityState.Modified;
|
Entry(entity).State = EntityState.Modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//find in currentDb
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
{
|
{
|
||||||
entity = await Set<T>().FirstOrDefaultAsync(predicate);
|
entity = await Set<T>().FirstOrDefaultAsync(predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//find in up-to-date db
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
{
|
{
|
||||||
var newAppDB = Activator.CreateInstance(this.GetType()) as CommonDbContext;
|
var newAppDB = CreateCopy();
|
||||||
|
|
||||||
entity = await newAppDB.Set<T>().FirstOrDefaultAsync(predicate);
|
entity = await newAppDB.Set<T>().FirstOrDefaultAsync(predicate);
|
||||||
|
|
||||||
|
|
@ -98,6 +103,8 @@ namespace NejCommon.Models;
|
||||||
if (entity != null && this.Entry(entity).State == EntityState.Detached)
|
if (entity != null && this.Entry(entity).State == EntityState.Detached)
|
||||||
Attach(entity);
|
Attach(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//create if not found
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
{
|
{
|
||||||
var newEntity = factory();
|
var newEntity = factory();
|
||||||
|
|
@ -110,15 +117,30 @@ namespace NejCommon.Models;
|
||||||
public T FindOrCreate<T>(Expression<Func<T, bool>> predicate, Func<T> factory) where T : class
|
public T FindOrCreate<T>(Expression<Func<T, bool>> predicate, Func<T> factory) where T : class
|
||||||
{
|
{
|
||||||
var entity = ChangeTracker.Entries<T>().Where(e => e.State != EntityState.Deleted).Select(x => x.Entity).FirstOrDefault(predicate.Compile());
|
var entity = ChangeTracker.Entries<T>().Where(e => e.State != EntityState.Deleted).Select(x => x.Entity).FirstOrDefault(predicate.Compile());
|
||||||
//var entity = this.;
|
|
||||||
|
//find in change tracker
|
||||||
|
if (entity != null && Entry(entity).State == EntityState.Deleted)
|
||||||
|
{
|
||||||
|
Entry(entity).State = EntityState.Modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
//find in currentDb
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
{
|
{
|
||||||
var newAppDB = Activator.CreateInstance(this.GetType()) as CommonDbContext;
|
entity = Set<T>().FirstOrDefault(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
//find in up-to-date db
|
||||||
|
if (entity == null)
|
||||||
|
{
|
||||||
|
var newAppDB = CreateCopy();
|
||||||
|
|
||||||
entity = newAppDB.Set<T>().FirstOrDefault(predicate);
|
entity = newAppDB.Set<T>().FirstOrDefault(predicate);
|
||||||
if (entity != null)
|
if (entity != null)
|
||||||
Attach(entity);
|
Attach(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//create if not found
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
{
|
{
|
||||||
var newEntity = factory();
|
var newEntity = factory();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user