mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
#694 ✨ Exception信息 国际化 多语言
This commit is contained in:
@ -140,11 +140,11 @@ namespace FreeSql
|
||||
/// <returns></returns>
|
||||
public DbSet<TEntity> AsType(Type entityType)
|
||||
{
|
||||
if (entityType == typeof(object)) throw new Exception("ISelect.AsType 参数不支持指定为 object");
|
||||
if (entityType == typeof(object)) throw new Exception(CoreStrings.TypeAsType_NotSupport_Object("DbSet"));
|
||||
if (entityType == _entityType) return this;
|
||||
var newtb = _db.OrmOriginal.CodeFirst.GetTableByEntity(entityType);
|
||||
_entityType = entityType;
|
||||
_tablePriv = newtb ?? throw new Exception("DbSet.AsType 参数错误,请传入正确的实体类型");
|
||||
_tablePriv = newtb ?? throw new Exception(CoreStrings.Type_AsType_Parameter_Error("DbSet"));
|
||||
_tableIdentitysPriv = null;
|
||||
_tableReturnColumnsPriv = null;
|
||||
return this;
|
||||
@ -197,11 +197,11 @@ namespace FreeSql
|
||||
public void AttachRange(IEnumerable<TEntity> data)
|
||||
{
|
||||
if (data == null || data.Any() == false) return;
|
||||
if (_table.Primarys.Any() == false) throw new Exception($"不可附加,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data.First())}");
|
||||
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotAttach_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data.First())));
|
||||
foreach (var item in data)
|
||||
{
|
||||
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, item, false);
|
||||
if (string.IsNullOrEmpty(key)) throw new Exception($"不可附加,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, item)}");
|
||||
if (string.IsNullOrEmpty(key)) throw new Exception(DbContextStrings.CannotAttach_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, item)));
|
||||
|
||||
_states.AddOrUpdate(key, k => CreateEntityState(item), (k, ov) =>
|
||||
{
|
||||
@ -236,9 +236,9 @@ namespace FreeSql
|
||||
public Dictionary<string, object[]> CompareState(TEntity newdata)
|
||||
{
|
||||
if (newdata == null) return null;
|
||||
if (_table.Primarys.Any() == false) throw new Exception($"不可比较,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, newdata)}");
|
||||
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.Incomparable_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, newdata)));
|
||||
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, newdata, false);
|
||||
if (string.IsNullOrEmpty(key)) throw new Exception($"不可比较,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, newdata)}");
|
||||
if (string.IsNullOrEmpty(key)) throw new Exception(DbContextStrings.Incomparable_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, newdata)));
|
||||
if (_states.TryGetValue(key, out var oldState) == false || oldState == null)
|
||||
return _table.ColumnsByCs.ToDictionary(a => a.Key, a => new object[]
|
||||
{
|
||||
@ -298,7 +298,7 @@ namespace FreeSql
|
||||
}
|
||||
if (_table.Primarys.Any() == false)
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可添加,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotAdd_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
FreeSql.Internal.CommonProvider.InsertProvider<TEntity>.AuditDataValue(this, data, _db.OrmOriginal, _table, null);
|
||||
@ -319,7 +319,7 @@ namespace FreeSql
|
||||
default:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1)
|
||||
return true;
|
||||
if (isThrow) throw new Exception($"不可添加,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotAdd_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -327,14 +327,14 @@ namespace FreeSql
|
||||
{
|
||||
if (_states.ContainsKey(key))
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可添加,已存在于状态管理:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotAdd_AlreadyExistsInStateManagement(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
if (_db.OrmOriginal.Ado.DataType == DataType.ClickHouse) return true;
|
||||
var idval = _db.OrmOriginal.GetEntityIdentityValueWithPrimary(_entityType, data);
|
||||
if (idval > 0)
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可添加,自增属性有值:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotAdd_SelfIncreasingHasValue(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -361,19 +361,19 @@ namespace FreeSql
|
||||
}
|
||||
if (_table.Primarys.Any() == false)
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可更新,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotUpdate_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
FreeSql.Internal.CommonProvider.UpdateProvider<TEntity>.AuditDataValue(this, data, _db.OrmOriginal, _table, null);
|
||||
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, data, false);
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可更新,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotUpdate_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
if (_states.TryGetValue(key, out var tryval) == false)
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可更新,数据未被跟踪,应该先查询 或者 Attach:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotUpdate_DataShouldQueryOrAttach(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -399,13 +399,13 @@ namespace FreeSql
|
||||
}
|
||||
if (_table.Primarys.Any() == false)
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可删除,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotDelete_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, data, false);
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
if (isThrow) throw new Exception($"不可删除,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (isThrow) throw new Exception(DbContextStrings.CannotDelete_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
return false;
|
||||
}
|
||||
//if (_states.TryGetValue(key, out var tryval) == false) {
|
||||
|
@ -114,7 +114,7 @@ namespace FreeSql
|
||||
case DataType.ShenTong:
|
||||
await DbContextFlushCommandAsync(cancellationToken);
|
||||
var rets = await this.OrmInsert(data).ExecuteInsertedAsync(cancellationToken);
|
||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_db.OrmOriginal.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||
if (rets.Count != data.Count()) throw new Exception(DbContextStrings.SpecialError_BatchAdditionFailed(_db.OrmOriginal.Ado.DataType));
|
||||
_db._entityChangeReport.AddRange(rets.Select(a => new DbContext.EntityChangeReport.ChangeInfo { Object = a, Type = DbContext.EntityChangeType.Insert }));
|
||||
var idx = 0;
|
||||
foreach (var s in data)
|
||||
@ -148,8 +148,8 @@ namespace FreeSql
|
||||
{
|
||||
if (item == null) return;
|
||||
if (string.IsNullOrEmpty(propertyName)) return;
|
||||
if (_table.Properties.TryGetValue(propertyName, out var prop) == false) throw new KeyNotFoundException($"{_table.Type.FullName} 不存在属性 {propertyName}");
|
||||
if (_table.ColumnsByCsIgnore.ContainsKey(propertyName)) throw new ArgumentException($"{_table.Type.FullName} 类型已设置属性 {propertyName} 忽略特性");
|
||||
if (_table.Properties.TryGetValue(propertyName, out var prop) == false) throw new KeyNotFoundException(DbContextStrings.NotFound_Property(_table.Type.FullName, propertyName));
|
||||
if (_table.ColumnsByCsIgnore.ContainsKey(propertyName)) throw new ArgumentException(DbContextStrings.TypeHasSetProperty_IgnoreAttribute(_table.Type.FullName, propertyName));
|
||||
|
||||
var tref = _table.GetTableRef(propertyName, true);
|
||||
if (tref == null) return;
|
||||
@ -157,7 +157,7 @@ namespace FreeSql
|
||||
{
|
||||
case Internal.Model.TableRefType.OneToOne:
|
||||
case Internal.Model.TableRefType.ManyToOne:
|
||||
throw new ArgumentException($"{_table.Type.FullName} 类型的属性 {propertyName} 不是 OneToMany 或 ManyToMany 特性");
|
||||
throw new ArgumentException(DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany(_table.Type.FullName, propertyName));
|
||||
}
|
||||
|
||||
await DbContextFlushCommandAsync(cancellationToken);
|
||||
@ -363,7 +363,7 @@ namespace FreeSql
|
||||
|
||||
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
||||
var lstval2 = default(EntityState);
|
||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_db.OrmOriginal.GetEntityString(_entityType, uplst2.Value)}");
|
||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception(DbContextStrings.SpecialError_UpdateFailedDataNotTracked(_db.OrmOriginal.GetEntityString(_entityType, uplst2.Value)));
|
||||
|
||||
var cuig1 = _db.OrmOriginal.CompareEntityValueReturnColumns(_entityType, uplst1.Value, lstval1.Value, true);
|
||||
var cuig2 = uplst2 != null ? _db.OrmOriginal.CompareEntityValueReturnColumns(_entityType, uplst2.Value, lstval2.Value, true) : null;
|
||||
@ -413,11 +413,11 @@ namespace FreeSql
|
||||
async public Task UpdateAsync(TEntity data, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var exists = ExistsInStates(data);
|
||||
if (exists == null) throw new Exception($"不可更新,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (exists == null) throw new Exception(DbContextStrings.CannotUpdate_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
if (exists == false)
|
||||
{
|
||||
var olddata = await OrmSelect(data).FirstAsync(cancellationToken);
|
||||
if (olddata == null) throw new Exception($"不可更新,数据库不存在该记录:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (olddata == null) throw new Exception(DbContextStrings.CannotUpdate_RecordDoesNotExist(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
}
|
||||
|
||||
await UpdateRangePrivAsync(new[] { data }, true, cancellationToken);
|
||||
@ -472,7 +472,7 @@ namespace FreeSql
|
||||
async public Task AddOrUpdateAsync(TEntity data, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (data == null) throw new ArgumentNullException(nameof(data));
|
||||
if (_table.Primarys.Any() == false) throw new Exception($"不可添加,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotAdd_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
|
||||
var flagExists = ExistsInStates(data);
|
||||
if (flagExists == false)
|
||||
|
@ -116,7 +116,7 @@ namespace FreeSql
|
||||
case DataType.ShenTong:
|
||||
DbContextFlushCommand();
|
||||
var rets = this.OrmInsert(data).ExecuteInserted();
|
||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_db.OrmOriginal.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||
if (rets.Count != data.Count()) throw new Exception(DbContextStrings.SpecialError_BatchAdditionFailed(_db.OrmOriginal.Ado.DataType));
|
||||
_db._entityChangeReport.AddRange(rets.Select(a => new DbContext.EntityChangeReport.ChangeInfo { Object = a, Type = DbContext.EntityChangeType.Insert }));
|
||||
var idx = 0;
|
||||
foreach (var s in data)
|
||||
@ -159,8 +159,8 @@ namespace FreeSql
|
||||
{
|
||||
if (item == null) return;
|
||||
if (string.IsNullOrEmpty(propertyName)) return;
|
||||
if (_table.Properties.TryGetValue(propertyName, out var prop) == false) throw new KeyNotFoundException($"{_table.Type.FullName} 不存在属性 {propertyName}");
|
||||
if (_table.ColumnsByCsIgnore.ContainsKey(propertyName)) throw new ArgumentException($"{_table.Type.FullName} 类型已设置属性 {propertyName} 忽略特性");
|
||||
if (_table.Properties.TryGetValue(propertyName, out var prop) == false) throw new KeyNotFoundException(DbContextStrings.NotFound_Property(_table.Type.FullName, propertyName));
|
||||
if (_table.ColumnsByCsIgnore.ContainsKey(propertyName)) throw new ArgumentException(DbContextStrings.TypeHasSetProperty_IgnoreAttribute(_table.Type.FullName, propertyName));
|
||||
|
||||
var tref = _table.GetTableRef(propertyName, true);
|
||||
if (tref == null) return;
|
||||
@ -168,7 +168,7 @@ namespace FreeSql
|
||||
{
|
||||
case Internal.Model.TableRefType.OneToOne:
|
||||
case Internal.Model.TableRefType.ManyToOne:
|
||||
throw new ArgumentException($"{_table.Type.FullName} 类型的属性 {propertyName} 不是 OneToMany 或 ManyToMany 特性");
|
||||
throw new ArgumentException(DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany(_table.Type.FullName, propertyName));
|
||||
}
|
||||
|
||||
DbContextFlushCommand();
|
||||
@ -397,7 +397,7 @@ namespace FreeSql
|
||||
|
||||
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
||||
var lstval2 = default(EntityState);
|
||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_db.OrmOriginal.GetEntityString(_entityType, uplst2.Value)}");
|
||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception(DbContextStrings.SpecialError_UpdateFailedDataNotTracked(_db.OrmOriginal.GetEntityString(_entityType, uplst2.Value)));
|
||||
|
||||
var cuig1 = _db.OrmOriginal.CompareEntityValueReturnColumns(_entityType, uplst1.Value, lstval1.Value, true);
|
||||
var cuig2 = uplst2 != null ? _db.OrmOriginal.CompareEntityValueReturnColumns(_entityType, uplst2.Value, lstval2.Value, true) : null;
|
||||
@ -454,11 +454,11 @@ namespace FreeSql
|
||||
public void Update(TEntity data)
|
||||
{
|
||||
var exists = ExistsInStates(data);
|
||||
if (exists == null) throw new Exception($"不可更新,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (exists == null) throw new Exception(DbContextStrings.CannotUpdate_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
if (exists == false)
|
||||
{
|
||||
var olddata = OrmSelect(data).First();
|
||||
if (olddata == null) throw new Exception($"不可更新,数据库不存在该记录:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (olddata == null) throw new Exception(DbContextStrings.CannotUpdate_RecordDoesNotExist(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
}
|
||||
|
||||
UpdateRangePriv(new[] { data }, true);
|
||||
@ -545,7 +545,7 @@ namespace FreeSql
|
||||
public void AddOrUpdate(TEntity data)
|
||||
{
|
||||
if (data == null) throw new ArgumentNullException(nameof(data));
|
||||
if (_table.Primarys.Any() == false) throw new Exception($"不可添加,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
|
||||
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotAdd_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data)));
|
||||
|
||||
var flagExists = ExistsInStates(data);
|
||||
if (flagExists == false)
|
||||
@ -585,7 +585,7 @@ namespace FreeSql
|
||||
public void BeginEdit(List<TEntity> data)
|
||||
{
|
||||
if (data == null) return;
|
||||
if (_table.Primarys.Any() == false) throw new Exception($"不可进行编辑,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data.First())}");
|
||||
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotEdit_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data.First())));
|
||||
_statesEditing.Clear();
|
||||
_dataEditing = data;
|
||||
foreach (var item in data)
|
||||
@ -875,7 +875,7 @@ namespace FreeSql
|
||||
|
||||
var rawset = _db.Set(dbset.EntityType);
|
||||
var statesRemove = typeof(DbSet<>).MakeGenericType(dbset.EntityType).GetMethod("StatesRemoveByObjects", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(IEnumerable<object>) }, null);
|
||||
if (statesRemove == null) throw new Exception("找不到方法 DbSet<>.StatesRemoveByObjects");
|
||||
if (statesRemove == null) throw new Exception(DbContextStrings.NotFoundMethod_StatesRemoveByObjects);
|
||||
statesRemove.Invoke(rawset, new object[] { items });
|
||||
}
|
||||
returnDeleted?.AddRange(items);
|
||||
|
Reference in New Issue
Block a user