- 修复 LazyLoading 在 Net4 环境下的问题;

This commit is contained in:
28810 2019-12-19 10:51:21 +08:00
parent 954a948837
commit e03aaed55b
8 changed files with 47 additions and 20 deletions

View File

@ -0,0 +1 @@


View File

@ -31,7 +31,7 @@ public class User1 : BaseEntity<User1, Guid>
public int GroupId { get; set; }
public UserGroup Group { get; set; }
public List<Role> Roles { get; set; }
public virtual List<Role> Roles { get; set; }
/// <summary>
/// 登陆名

View File

@ -44,10 +44,13 @@ namespace base_entity
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db;max pool size=5")
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3")
.UseLazyLoading(true)
.Build();
BaseEntity.Initialization(fsql);
#endregion
var us = User1.Select.Limit(10).ToList();
new Products { title = "product-1" }.Save();
new Products { title = "product-2" }.Save();
new Products { title = "product-3" }.Save();

View File

@ -17,6 +17,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.BaseEntity\FreeSql.Extensions.BaseEntity.csproj" />
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.JsonMap\FreeSql.Extensions.JsonMap.csproj" />
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.MySql\FreeSql.Provider.MySql.csproj" />

View File

@ -35,13 +35,6 @@ namespace orm_vs
static void Main(string[] args)
{
var testlist1 = fsql.Select<Song>().OrderBy(a => a.Id).ToList();
var testlist2 = new List<Song>();
fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(0, list =>
{
testlist2.AddRange(list);
});
fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag));
//sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag));
//sugar创建表失败SqlSugar.SqlSugarException: Sequence contains no elements

View File

@ -1,9 +1,7 @@
using Microsoft.CSharp;
using System;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace FreeSql.Extensions.LazyLoading
{
@ -14,7 +12,7 @@ namespace FreeSql.Extensions.LazyLoading
#if ns20
internal static Lazy<CSScriptLib.RoslynEvaluator> _compiler = new Lazy<CSScriptLib.RoslynEvaluator>(() =>
{
//var dlls = Directory.GetFiles(Directory.GetParent(Type.GetType("IFreeSql, FreeSql").Assembly.Location).FullName, "*.dll");
//var dlls = Directory.GetFiles(Directory.GetParent(Type.GetType("IFreeSql, FreeSql").Assembly.Location).FullName, "*.dll;*.exe");
var compiler = new CSScriptLib.RoslynEvaluator();
compiler.DisableReferencingFromCode = false;
//compiler.DebugBuild = true;
@ -38,11 +36,38 @@ namespace FreeSql.Extensions.LazyLoading
public static Assembly CompileCode(string cscode) {
var files = Directory.GetFiles(Directory.GetParent(Type.GetType("IFreeSql, FreeSql").Assembly.Location).FullName);
using (var compiler = CodeDomProvider.CreateProvider("cs")) {
var objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Core.dll");
objCompilerParameters.ReferencedAssemblies.Add("FreeSql.dll");
foreach (var dll in files)
{
if (!dll.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) &&
!dll.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) continue;
Console.WriteLine(dll);
var dllName = string.Empty;
var idx = dll.LastIndexOf('/');
if (idx != -1) dllName = dll.Substring(idx + 1);
else
{
idx = dll.LastIndexOf('\\');
if (idx != -1) dllName = dll.Substring(idx + 1);
}
if (string.IsNullOrEmpty(dllName)) continue;
try
{
var ass = Assembly.LoadFile(dll);
objCompilerParameters.ReferencedAssemblies.Add(dllName);
}
catch
{
}
}
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;

View File

@ -788,8 +788,8 @@ namespace FreeSql.Internal
.Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {");
if (nvref.Exception == null)
cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.IsNested ? $"{propElementType.DeclaringType.Namespace}.{propElementType.DeclaringType.Name}.{propElementType.Name}" : $"{propElementType.Namespace}.{propElementType.Name}")
.Append(">().Where(a => __fsql_orm__.Select<").Append(tbmid.Type.IsNested ? $"{tbmid.Type.DeclaringType.Namespace?.NotNullAndConcat(".")}{tbmid.Type.DeclaringType.Name}.{tbmid.Type.Name}" : $"{tbmid.Type.Namespace}.{tbmid.Type.Name}")
cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.IsNested ? $"{propElementType.DeclaringType.Namespace?.NotNullAndConcat(".")}{propElementType.DeclaringType.Name}.{propElementType.Name}" : $"{propElementType.Namespace?.NotNullAndConcat(".")}{propElementType.Name}")
.Append(">().Where(a => __fsql_orm__.Select<").Append(tbmid.Type.IsNested ? $"{tbmid.Type.DeclaringType.Namespace?.NotNullAndConcat(".")}{tbmid.Type.DeclaringType.Name}.{tbmid.Type.Name}" : $"{tbmid.Type.Namespace?.NotNullAndConcat(".")}{tbmid.Type.Name}")
.Append(">().Where(b => ").Append(lmbdWhere.ToString()).AppendLine(").Any()).ToList();")
.Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;");
else
@ -801,7 +801,9 @@ namespace FreeSql.Internal
}
if (vp?.Item3 == true)
{ //set 重写
cscode.Append(" set => base.").Append(pnv.Name).AppendLine(" = value;");
cscode.Append(" set {\r\n")
.Append(" base.").Append(pnv.Name).AppendLine(" = value;")
.Append(" }\r\n");
}
cscode.AppendLine(" }");
}
@ -940,7 +942,9 @@ namespace FreeSql.Internal
}
if (vp?.Item3 == true)
{ //set 重写
cscode.Append(" set => base.").Append(pnv.Name).AppendLine(" = value;");
cscode.Append(" set {\r\n")
.Append(" base.").Append(pnv.Name).AppendLine(" = value;")
.Append(" }\r\n");
}
cscode.AppendLine(" }");
}