Skip to content

Commit a3978d7

Browse files
committed
- 修复 Firebird Inserted/Deleted 与 Repository 级联冲突 bug;#2023
1 parent 84461f1 commit a3978d7

File tree

8 files changed

+119
-17
lines changed

8 files changed

+119
-17
lines changed

FreeSql-DbContext.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.DbContext2",
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Extensions.AggregateRoot", "Extensions\FreeSql.Extensions.AggregateRoot\FreeSql.Extensions.AggregateRoot.csproj", "{B8F84E4F-46F2-4048-B79B-49F0B8A95335}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeSql.Extensions.ZeroEntity", "Extensions\FreeSql.Extensions.ZeroEntity\FreeSql.Extensions.ZeroEntity.csproj", "{9F309623-51D0-9A59-9FA9-0AE166E30B0C}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Extensions.ZeroEntity", "Extensions\FreeSql.Extensions.ZeroEntity\FreeSql.Extensions.ZeroEntity.csproj", "{9F309623-51D0-9A59-9FA9-0AE166E30B0C}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeSql.Extensions.EFModel", "Extensions\FreeSql.Extensions.EFModel\FreeSql.Extensions.EFModel.csproj", "{A05882CE-3E20-40C0-AE2E-9736848198BE}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Extensions.EFModel", "Extensions\FreeSql.Extensions.EFModel\FreeSql.Extensions.EFModel.csproj", "{A05882CE-3E20-40C0-AE2E-9736848198BE}"
21+
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.Firebird", "Providers\FreeSql.Provider.Firebird\FreeSql.Provider.Firebird.csproj", "{5CD37013-EDE0-4901-9169-859A48F25BCE}"
2123
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -57,6 +59,10 @@ Global
5759
{A05882CE-3E20-40C0-AE2E-9736848198BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
5860
{A05882CE-3E20-40C0-AE2E-9736848198BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
5961
{A05882CE-3E20-40C0-AE2E-9736848198BE}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{5CD37013-EDE0-4901-9169-859A48F25BCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{5CD37013-EDE0-4901-9169-859A48F25BCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{5CD37013-EDE0-4901-9169-859A48F25BCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
65+
{5CD37013-EDE0-4901-9169-859A48F25BCE}.Release|Any CPU.Build.0 = Release|Any CPU
6066
EndGlobalSection
6167
GlobalSection(SolutionProperties) = preSolution
6268
HideSolutionNode = FALSE

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.Tests/FreeSql.Tests.DbContext2/FreeSql.Tests.DbContext2.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
2828
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
2929
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
30+
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Firebird\FreeSql.Provider.Firebird.csproj" />
3031
</ItemGroup>
3132

3233
</Project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using FreeSql.DataAnnotations;
2+
using FreeSql.Internal;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.ComponentModel.DataAnnotations;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Xunit;
10+
11+
namespace FreeSql.Tests.DbContext2
12+
{
13+
public class Issues2023Test
14+
{
15+
16+
[Fact]
17+
public void Test2023()
18+
{
19+
var fsql = g.firebird;
20+
var list = new List<TestCategory>
21+
{
22+
new TestCategory
23+
{
24+
Name = "c1",
25+
Articles = new List<TestArticle>
26+
{
27+
new TestArticle
28+
{
29+
Title = "t1a",
30+
},
31+
new TestArticle
32+
{
33+
Title = "t1b",
34+
}
35+
}
36+
},
37+
new TestCategory
38+
{
39+
Name = "c2",
40+
Articles = new List<TestArticle>
41+
{
42+
new TestArticle
43+
{
44+
Title = "t2a",
45+
},
46+
new TestArticle
47+
{
48+
Title = "t2b",
49+
}
50+
}
51+
}
52+
};
53+
54+
var repo = fsql.GetRepository<TestCategory>();
55+
repo.DbContextOptions.EnableCascadeSave = true;
56+
repo.Insert(list); // 这里报 System.NullReferenceException
57+
}
58+
59+
[Table(Name = "test_category")]
60+
public class TestCategory
61+
{
62+
[Column(IsPrimary = true, IsIdentity = true)]
63+
public long Id { get; set; }
64+
65+
[Required]
66+
public string Name { get; set; }
67+
68+
[Navigate(nameof(TestArticle.CategoryId))]
69+
public List<TestArticle> Articles { get; set; } = new List<TestArticle>();
70+
}
71+
72+
[Table(Name = "test_article")]
73+
public class TestArticle
74+
{
75+
[Column(IsPrimary = true, IsIdentity = true)]
76+
public long Id { get; set; }
77+
78+
[Required]
79+
public string Title { get; set; }
80+
81+
public long CategoryId { get; set; }
82+
83+
[Navigate(nameof(CategoryId))]
84+
public TestCategory Category { get; set; }
85+
86+
[Column(ServerTime = DateTimeKind.Local)] // 移除此特性就可以成功插入!
87+
public DateTime CreatedTime { get; set; }
88+
}
89+
}
90+
}

FreeSql.Tests/FreeSql.Tests.DbContext2/g.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ public static IFreeSql CreateMemory()
3232
.Build();
3333
}
3434

35+
static Lazy<IFreeSql> firebirdLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
36+
.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
37+
//.UseConnectionFactory(FreeSql.DataType.Firebird, () => new FirebirdSql.Data.FirebirdClient.FbConnection(@"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5"))
38+
.UseAutoSyncStructure(true)
39+
.UseLazyLoading(true)
40+
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
41+
//.UseNoneCommandParameter(true)
42+
43+
.UseMonitorCommand(
44+
cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象,在执行前
45+
(cmd, traceLog) => Console.WriteLine(traceLog))
46+
.Build());
47+
public static IFreeSql firebird => firebirdLazy.Value;
3548
}

FreeSql/FreeSql.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Providers/FreeSql.Provider.Firebird/Curd/FirebirdDelete.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public override List<T1> ExecuteDeleted()
3939
}
4040
}
4141
var sql = sb.Append(sbret).ToString();
42-
var before = new Aop.CurdBeforeEventArgs(_table.TypeLazy ?? _table.Type, _table, Aop.CurdType.Delete, sql, dbParms);
42+
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, sql, dbParms);
4343
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
4444

4545
Exception exception = null;
4646
try
4747
{
48-
ret.AddRange(_orm.Ado.Query<T1>(_connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms));
48+
ret.AddRange(_orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms));
4949
}
5050
catch (Exception ex)
5151
{
@@ -90,13 +90,13 @@ await ToSqlFetchAsync(async sb =>
9090
}
9191
}
9292
var sql = sb.Append(sbret).ToString();
93-
var before = new Aop.CurdBeforeEventArgs(_table.TypeLazy ?? _table.Type, _table, Aop.CurdType.Delete, sql, dbParms);
93+
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, sql, dbParms);
9494
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
9595

9696
Exception exception = null;
9797
try
9898
{
99-
ret.AddRange(await _orm.Ado.QueryAsync<T1>(_connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken));
99+
ret.AddRange(await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken));
100100
}
101101
catch (Exception ex)
102102
{

Providers/FreeSql.Provider.Firebird/Curd/FirebirdInsert.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected override List<T1> RawExecuteInserted()
107107
Exception exception = null;
108108
try
109109
{
110-
ret = _orm.Ado.Query<T1>(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params);
110+
ret = _orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, _params);
111111
}
112112
catch (Exception ex)
113113
{
@@ -207,7 +207,7 @@ async protected override Task<List<T1>> RawExecuteInsertedAsync(CancellationToke
207207
Exception exception = null;
208208
try
209209
{
210-
ret = await _orm.Ado.QueryAsync<T1>(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params, cancellationToken);
210+
ret = await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, _params, cancellationToken);
211211
}
212212
catch (Exception ex)
213213
{

0 commit comments

Comments
 (0)