AggregateRootRepository

This commit is contained in:
2881099 2022-09-02 23:41:04 +08:00
parent 32777b7543
commit abfb8232c8
3 changed files with 22 additions and 18 deletions

View File

@ -52,7 +52,6 @@ namespace FreeSql
_repository.AsTable(rule); _repository.AsTable(rule);
_asTableRule = rule; _asTableRule = rule;
} }
TableInfo _table;
public Type EntityType => _repository.EntityType; public Type EntityType => _repository.EntityType;
public IDataFilter<TEntity> DataFilter => _repository.DataFilter; public IDataFilter<TEntity> DataFilter => _repository.DataFilter;

View File

@ -156,7 +156,8 @@ namespace FreeSql
{ {
var stateKey = Orm.GetEntityKeyString(EntityType, entity, false); var stateKey = Orm.GetEntityKeyString(EntityType, entity, false);
if (entity == null) throw new ArgumentNullException(nameof(entity)); if (entity == null) throw new ArgumentNullException(nameof(entity));
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotAdd_EntityHasNo_PrimaryKey(Orm.GetEntityString(EntityType, entity))); var table = Orm.CodeFirst.GetTableByEntity(EntityType);
if (table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotAdd_EntityHasNo_PrimaryKey(Orm.GetEntityString(EntityType, entity)));
var flagExists = ExistsInStates(entity); var flagExists = ExistsInStates(entity);
if (flagExists == false) if (flagExists == false)
@ -169,9 +170,13 @@ namespace FreeSql
var affrows = UpdateAggregateRoot(new[] { entity }); var affrows = UpdateAggregateRoot(new[] { entity });
if (affrows > 0) return entity; if (affrows > 0) return entity;
} }
if (table.Primarys.Where(a => a.Attribute.IsIdentity).Count() == table.Primarys.Length)
{
Orm.ClearEntityPrimaryValueWithIdentity(EntityType, entity); Orm.ClearEntityPrimaryValueWithIdentity(EntityType, entity);
return InsertAggregateRoot(new[] { entity }).FirstOrDefault(); return InsertAggregateRoot(new[] { entity }).FirstOrDefault();
} }
throw new Exception(DbContextStrings.CannotAdd_PrimaryKey_NotSet(Orm.GetEntityString(EntityType, entity)));
}
protected virtual int UpdateAggregateRoot(IEnumerable<TEntity> entitys) protected virtual int UpdateAggregateRoot(IEnumerable<TEntity> entitys)
{ {
List<NativeTuple<Type, object>> insertLog = new List<NativeTuple<Type, object>>(); List<NativeTuple<Type, object>> insertLog = new List<NativeTuple<Type, object>>();

View File

@ -116,29 +116,29 @@ static class AggregateRootUtils
} }
if (collectionBefore != null && collectionAfter == null) if (collectionBefore != null && collectionAfter == null)
{ {
foreach (var item in collectionBefore as IEnumerable) //foreach (var item in collectionBefore as IEnumerable)
{ //{
deleteLog.Add(NativeTuple.Create(elementType, new[] { item })); // deleteLog.Add(NativeTuple.Create(elementType, new[] { item }));
NavigateReader(fsql, elementType, item, (path, tr, ct, stackvs) => // NavigateReader(fsql, elementType, item, (path, tr, ct, stackvs) =>
{ // {
var dellist = stackvs.First() as object[] ?? new [] { stackvs.First() }; // var dellist = stackvs.First() as object[] ?? new [] { stackvs.First() };
deleteLog.Add(NativeTuple.Create(ct, dellist)); // deleteLog.Add(NativeTuple.Create(ct, dellist));
}); // });
} //}
return; return;
} }
Dictionary<string, object> dictBefore = new Dictionary<string, object>(); Dictionary<string, object> dictBefore = new Dictionary<string, object>();
Dictionary<string, object> dictAfter = new Dictionary<string, object>(); Dictionary<string, object> dictAfter = new Dictionary<string, object>();
foreach (var item in collectionBefore as IEnumerable) foreach (var item in collectionBefore as IEnumerable)
{ {
var beforeKey = fsql.GetEntityKeyString(elementType, item, false); var key = fsql.GetEntityKeyString(elementType, item, false);
dictBefore.Add(beforeKey, item); if (key != null) dictBefore.Add(key, item);
} }
foreach (var item in collectionAfter as IEnumerable) foreach (var item in collectionAfter as IEnumerable)
{ {
var afterKey = fsql.GetEntityKeyString(elementType, item, false); var key = fsql.GetEntityKeyString(elementType, item, false);
if (afterKey != null) insertLog.Add(NativeTuple.Create(elementType, item)); if (key != null) insertLog.Add(NativeTuple.Create(elementType, item));
else dictBefore.Add(afterKey, item); else dictAfter.Add(key, item);
} }
foreach (var key in dictBefore.Keys.ToArray()) foreach (var key in dictBefore.Keys.ToArray())
{ {