Skip to content

Commit

Permalink
- 优化 DbSet/Repository Attach 与 CanUpdate AuditValue 状态不同步问题;#1746
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Mar 9, 2024
1 parent 1f7e978 commit e1f91ef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
45 changes: 44 additions & 1 deletion Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using FreeSql.Odbc.Default;
using MessagePack;
using Microsoft.Data.SqlClient;
using MySqlConnector;
using NetTopologySuite.Geometries;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -571,7 +572,7 @@ static void Main(string[] args)

//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")

//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
//.UseAdoConnectionPool(false)
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
////.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=toc;Pooling=true;Maximum Pool Size=2")
Expand Down Expand Up @@ -611,6 +612,18 @@ static void Main(string[] args)
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion

fsql.Aop.AuditValue += (_, e) =>
{

};

var tt1 = new ProjectItem { ID = 1, MaxQuantity = 0, Code = null, Name = null };
var tt2 = new ProjectItem { ID = 1, MaxQuantity = 100, Code = null, Name = null };
var repot2 = fsql.GetRepository<ProjectItem>();

repot2.Attach(tt1);
var nt1 = repot2.Update(tt2);

var fsql2 = fsql;
// 动态构建实体类型,树形结构,引用自身类型
var areaBuilder = fsql2.CodeFirst.DynamicEntity("Area", new TableAttribute { Name = "dy_area" });
Expand Down Expand Up @@ -3192,4 +3205,34 @@ public class VM_District_Parent : BaseDistrict

[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}

[JsonObject(MemberSerialization.OptIn), Table(DisableSyncStructure = true)]
public partial class ProjectItem
{
[JsonProperty, Column(DbType = "bigint", IsPrimary = true, IsIdentity = true)]
public long ID { get; set; }


/// <summary>
/// 编码
/// </summary>
[JsonProperty, Column(StringLength = 100, IsNullable = false)]
public string Code { get; set; }


/// <summary>
/// 实际最大用量
/// </summary>
[JsonProperty, Column(DbType = "decimal(14,4)")]
public decimal MaxQuantity { get; set; } = 0.0000M;

/// <summary>
/// 名称
/// </summary>
[JsonProperty, Column(StringLength = 50, IsNullable = false)]
public string Name { get; set; }



}
15 changes: 15 additions & 0 deletions Examples/base_entity/base_entity.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions FreeSql.DbContext/DbSet/DbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public void AttachRange(IEnumerable<TEntity> data)
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotAttach_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data.First())));
foreach (var item in data)
{
FreeSql.Internal.CommonProvider.UpdateProvider<TEntity>.AuditDataValue(this, item, _db.OrmOriginal, _table, null); //与 CanUpdate 同步
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, item, false);
if (string.IsNullOrEmpty(key)) throw new Exception(DbContextStrings.CannotAttach_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, item)));

Expand Down

0 comments on commit e1f91ef

Please sign in to comment.