- 修复 InsertValueSql 在仓储插入后不返回最新值;

This commit is contained in:
2881099 2022-04-24 16:11:18 +08:00
parent 1cd5539655
commit 87c71d5be3
5 changed files with 40 additions and 13 deletions

View File

@ -6,6 +6,7 @@ using FreeSql.Internal.Model;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Odbc; using System.Data.Odbc;
using System.Data.SqlClient; using System.Data.SqlClient;
@ -230,6 +231,31 @@ namespace base_entity
CouponIds = a.CouponIds CouponIds = a.CouponIds
}); });
int LocalConcurrentDictionaryIsTypeKey(Type dictType, int level = 1)
{
if (dictType.IsGenericType == false) return 0;
if (dictType.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)) return 0;
var typeargs = dictType.GetGenericArguments();
if (typeargs[0] == typeof(Type) || typeargs[0] == typeof(ColumnInfo) || typeargs[0] == typeof(TableInfo)) return level;
if (level > 2) return 0;
return LocalConcurrentDictionaryIsTypeKey(typeargs[1], level + 1);
}
var fds = typeof(FreeSql.Internal.Utils).GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
.Where(a => LocalConcurrentDictionaryIsTypeKey(a.FieldType) > 0).ToArray();
var ttypes1 = typeof(IFreeSql).Assembly.GetTypes().Select(a => new
{
Type = a,
ConcurrentDictionarys = a.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
.Where(b => LocalConcurrentDictionaryIsTypeKey(b.FieldType) > 0).ToArray()
}).Where(a => a.ConcurrentDictionarys.Length > 0).ToArray();
var ttypes2 = typeof(IBaseRepository).Assembly.GetTypes().Select(a => new
{
Type = a,
ConcurrentDictionarys = a.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
.Where(b => LocalConcurrentDictionaryIsTypeKey(b.FieldType) > 0).ToArray()
}).Where(a => a.ConcurrentDictionarys.Length > 0).ToArray();
#region pgsql poco #region pgsql poco
// fsql.Aop.ParseExpression += (_, e) => // fsql.Aop.ParseExpression += (_, e) =>
// { // {

View File

@ -127,9 +127,9 @@ namespace FreeSql
protected ConcurrentDictionary<string, EntityState> _states = new ConcurrentDictionary<string, EntityState>(); protected ConcurrentDictionary<string, EntityState> _states = new ConcurrentDictionary<string, EntityState>();
TableInfo _tablePriv; TableInfo _tablePriv;
protected TableInfo _table => _tablePriv ?? (_tablePriv = _db.OrmOriginal.CodeFirst.GetTableByEntity(_entityType)); protected TableInfo _table => _tablePriv ?? (_tablePriv = _db.OrmOriginal.CodeFirst.GetTableByEntity(_entityType));
ColumnInfo[] _tableIdentitysPriv, _tableServerTimesPriv; ColumnInfo[] _tableIdentitysPriv, _tableReturnColumnsPriv;
protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray()); protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
protected ColumnInfo[] _tableServerTimes => _tableServerTimesPriv ?? (_tableServerTimesPriv = _table.Primarys.Where(a => a.Attribute.ServerTime != DateTimeKind.Unspecified).ToArray()); protected ColumnInfo[] _tableReturnColumns => _tableReturnColumnsPriv ?? (_tableReturnColumnsPriv = _table.ColumnsByPosition.Where(a => a.Attribute.IsPrimary && a.Attribute.IsIdentity || string.IsNullOrWhiteSpace(a.DbInsertValue) == false).ToArray());
protected Type _entityType = typeof(TEntity); protected Type _entityType = typeof(TEntity);
public Type EntityType => _entityType; public Type EntityType => _entityType;
@ -146,7 +146,7 @@ namespace FreeSql
_entityType = entityType; _entityType = entityType;
_tablePriv = newtb ?? throw new Exception("DbSet.AsType 参数错误,请传入正确的实体类型"); _tablePriv = newtb ?? throw new Exception("DbSet.AsType 参数错误,请传入正确的实体类型");
_tableIdentitysPriv = null; _tableIdentitysPriv = null;
_tableServerTimesPriv = null; _tableReturnColumnsPriv = null;
return this; return this;
} }

View File

@ -33,7 +33,7 @@ namespace FreeSql
async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellationToken) async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellationToken)
{ {
if (isCheck && CanAdd(data, true) == false) return; if (isCheck && CanAdd(data, true) == false) return;
if (_tableIdentitys.Length > 0) if (_tableReturnColumns.Length > 0)
{ {
//有自增,马上执行 //有自增,马上执行
switch (_db.OrmOriginal.Ado.DataType) switch (_db.OrmOriginal.Ado.DataType)
@ -46,7 +46,7 @@ namespace FreeSql
case DataType.OdbcKingbaseES: case DataType.OdbcKingbaseES:
case DataType.ShenTong: case DataType.ShenTong:
case DataType.Firebird: //firebird 只支持单条插入 returning case DataType.Firebird: //firebird 只支持单条插入 returning
if (_tableIdentitys.Length == 1) if (_tableIdentitys.Length == 1 && _tableReturnColumns.Length == 1)
{ {
await DbContextFlushCommandAsync(cancellationToken); await DbContextFlushCommandAsync(cancellationToken);
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync(cancellationToken); var idtval = await this.OrmInsert(data).ExecuteIdentityAsync(cancellationToken);
@ -98,7 +98,7 @@ namespace FreeSql
await AddAsync(data.First(), cancellationToken); await AddAsync(data.First(), cancellationToken);
return; return;
} }
if (_tableIdentitys.Length > 0) if (_tableReturnColumns.Length > 0)
{ {
//有自增,马上执行 //有自增,马上执行
switch (_db.OrmOriginal.Ado.DataType) switch (_db.OrmOriginal.Ado.DataType)

View File

@ -32,7 +32,7 @@ namespace FreeSql
void AddPriv(TEntity data, bool isCheck) void AddPriv(TEntity data, bool isCheck)
{ {
if (isCheck && CanAdd(data, true) == false) return; if (isCheck && CanAdd(data, true) == false) return;
if (_tableIdentitys.Length > 0) if (_tableReturnColumns.Length > 0)
{ {
//有自增,马上执行 //有自增,马上执行
switch (_db.OrmOriginal.Ado.DataType) switch (_db.OrmOriginal.Ado.DataType)
@ -45,7 +45,7 @@ namespace FreeSql
case DataType.OdbcKingbaseES: case DataType.OdbcKingbaseES:
case DataType.ShenTong: case DataType.ShenTong:
case DataType.Firebird: //firebird 只支持单条插入 returning case DataType.Firebird: //firebird 只支持单条插入 returning
if (_tableIdentitys.Length == 1) if (_tableIdentitys.Length == 1 && _tableReturnColumns.Length == 1)
{ {
DbContextFlushCommand(); DbContextFlushCommand();
var idtval = this.OrmInsert(data).ExecuteIdentity(); var idtval = this.OrmInsert(data).ExecuteIdentity();
@ -101,7 +101,7 @@ namespace FreeSql
Add(data.First()); Add(data.First());
return; return;
} }
if (_tableIdentitys.Length > 0) if (_tableReturnColumns.Length > 0)
{ {
//有自增,马上执行 //有自增,马上执行
switch (_db.OrmOriginal.Ado.DataType) switch (_db.OrmOriginal.Ado.DataType)

View File

@ -596,8 +596,9 @@ namespace FreeSql.Tests
[Fact] [Fact]
public void BeginEditIdentity() public void BeginEditIdentity()
{ {
g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows(); var fsql = g.sqlserver;
var repo = g.sqlite.GetRepository<BeginEdit02>(); fsql.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<BeginEdit02>();
var cts = new[] { var cts = new[] {
new BeginEdit02 { Name = "分类1" }, new BeginEdit02 { Name = "分类1" },
new BeginEdit02 { Name = "分类1_1" }, new BeginEdit02 { Name = "分类1_1" },
@ -617,8 +618,8 @@ namespace FreeSql.Tests
Assert.Equal(3, repo.EndEdit()); Assert.Equal(3, repo.EndEdit());
g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows(); fsql.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
repo = g.sqlite.GetRepository<BeginEdit02>(); repo = fsql.GetRepository<BeginEdit02>();
cts = repo.Select.ToList(); cts = repo.Select.ToList();
repo.BeginEdit(cts); repo.BeginEdit(cts);