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);
_asTableRule = rule;
}
TableInfo _table;
public Type EntityType => _repository.EntityType;
public IDataFilter<TEntity> DataFilter => _repository.DataFilter;

View File

@ -156,7 +156,8 @@ namespace FreeSql
{
var stateKey = Orm.GetEntityKeyString(EntityType, entity, false);
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);
if (flagExists == false)
@ -169,8 +170,12 @@ namespace FreeSql
var affrows = UpdateAggregateRoot(new[] { entity });
if (affrows > 0) return entity;
}
Orm.ClearEntityPrimaryValueWithIdentity(EntityType, entity);
return InsertAggregateRoot(new[] { entity }).FirstOrDefault();
if (table.Primarys.Where(a => a.Attribute.IsIdentity).Count() == table.Primarys.Length)
{
Orm.ClearEntityPrimaryValueWithIdentity(EntityType, entity);
return InsertAggregateRoot(new[] { entity }).FirstOrDefault();
}
throw new Exception(DbContextStrings.CannotAdd_PrimaryKey_NotSet(Orm.GetEntityString(EntityType, entity)));
}
protected virtual int UpdateAggregateRoot(IEnumerable<TEntity> entitys)
{

View File

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