修复 DbContext bug

This commit is contained in:
28810 2019-03-29 22:31:12 +08:00
parent 37b4b69b08
commit ff758f338c
5 changed files with 62 additions and 8 deletions

View File

@ -1,8 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Numerics;
using System.Threading.Tasks; using System.Threading.Tasks;
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -12,8 +16,47 @@ namespace dbcontext_01
{ {
public class Program public class Program
{ {
public static void Main(string[] args)
{ public class Song {
[Column(IsIdentity = true)]
public int Id { get; set; }
public string BigNumber { get; set; }
[Column(IsVersion = true)]//使用简单
public long versionRow { get; set; }
}
public class SongContext : DbContext {
public DbSet<Song> Songs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
builder.UseFreeSql(fsql);
}
}
static IFreeSql fsql;
public static void Main(string[] args) {
fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\dd2.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
.Build();
using (var ctx = new SongContext()) {
var song = new Song { BigNumber = "1000000000000000000" };
ctx.Songs.Add(song);
ctx.Songs.Update(song);
song.BigNumber = (BigInteger.Parse(song.BigNumber) + 1).ToString();
ctx.Songs.Update(song);
ctx.SaveChanges();
}
CreateWebHostBuilder(args).Build().Run(); CreateWebHostBuilder(args).Build().Run();
} }

View File

@ -59,11 +59,17 @@ namespace FreeSql {
states.RemoveAt(states.Count - 1); states.RemoveAt(states.Count - 1);
return; return;
} }
if (affrows == -998 || affrows == -997) { //没有执行更新
var laststate = states[states.Count - 1];
states.Clear();
if (affrows == -997) states.Add(laststate); //保留最后一个
}
if (affrows > 0) { if (affrows > 0) {
_affrows += affrows; _affrows += affrows;
var islastNotUpdated = states.Count != affrows; var islastNotUpdated = states.Count != affrows;
var laststate = states[states.Count - 1];
states.Clear(); states.Clear();
if (islastNotUpdated) states.Add(oldinfo.state); if (islastNotUpdated) states.Add(laststate); //保留最后一个
} }
}; };

View File

@ -76,6 +76,7 @@ namespace FreeSql {
this.Key = key; this.Key = key;
this.Time = DateTime.Now; this.Time = DateTime.Now;
} }
public TEntity OldValue { get; set; }
public TEntity Value { get; set; } public TEntity Value { get; set; }
public string Key { get; set; } public string Key { get; set; }
public DateTime Time { get; set; } public DateTime Time { get; set; }

View File

@ -156,7 +156,7 @@ namespace FreeSql {
if (data?.Count > 0) { if (data?.Count > 0) {
if (cuig.Length == _table.Columns.Count) if (cuig.Length == _table.Columns.Count)
return data.Count; return ups.Length == data.Count ? -998 : -997;
var updateSource = data.Select(a => a.Value).ToArray(); var updateSource = data.Select(a => a.Value).ToArray();
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig); var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
@ -166,6 +166,8 @@ namespace FreeSql {
foreach (var newval in data) { foreach (var newval in data) {
if (_states.TryGetValue(newval.Key, out var tryold)) if (_states.TryGetValue(newval.Key, out var tryold))
_fsql.MapEntityValue(newval.Value, tryold.Value); _fsql.MapEntityValue(newval.Value, tryold.Value);
if (newval.OldValue != null)
_fsql.MapEntityValue(newval.Value, newval.OldValue);
} }
return affrows; return affrows;
} }
@ -176,7 +178,9 @@ namespace FreeSql {
void UpdatePriv(TEntity data, bool isCheck) { void UpdatePriv(TEntity data, bool isCheck) {
if (isCheck && CanUpdate(data, true) == false) return; if (isCheck && CanUpdate(data, true) == false) return;
EnqueueAction(DbContext.ExecCommandInfoType.Update, this, typeof(EntityState), CreateEntityState(data)); var state = CreateEntityState(data);
state.OldValue = data;
EnqueueAction(DbContext.ExecCommandInfoType.Update, this, typeof(EntityState), state);
} }
public void Update(TEntity data) => UpdatePriv(data, true); public void Update(TEntity data) => UpdatePriv(data, true);
public void UpdateRange(TEntity[] data) { public void UpdateRange(TEntity[] data) {

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.27</Version> <Version>0.3.27.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors> <Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description> <Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>