Skip to content

Commit

Permalink
Add support EF Code First for EntityTableDataLoader
Browse files Browse the repository at this point in the history
Add support EF Code First for EntityTableDataLoader
  • Loading branch information
zzzprojects committed Mar 12, 2018
1 parent 2305b30 commit 45b0105
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 30 deletions.
4 changes: 3 additions & 1 deletion Main/Build/Build.cmd
Expand Up @@ -23,4 +23,6 @@ if not exist %msbuild% (
)

set EnableNugetPackageRestore=true
%msbuild% Build.targets /v:minimal /maxcpucount /nodeReuse:false %*
%msbuild% Build.targets /v:minimal /maxcpucount /nodeReuse:false %*

timeout /t 2
Binary file added Main/Build/Effort 1.3.3.nupkg
Binary file not shown.
Binary file added Main/Build/Effort.EF6 1.3.3.nupkg
Binary file not shown.
2 changes: 1 addition & 1 deletion Main/Build/Effort.EF6.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd">
<metadata>
<id>Effort.EF6</id>
<version>1.3.2</version>
<version>1.3.3</version>
<title />
<authors>ZZZ Projects, tamasflamich</authors>
<owners>ZZZ Projects</owners>
Expand Down
2 changes: 1 addition & 1 deletion Main/Build/Effort.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd">
<metadata>
<id>Effort</id>
<version>1.3.2</version>
<version>1.3.3</version>
<title />
<authors>ZZZ Projects, tamasflamich</authors>
<owners>ZZZ Projects</owners>
Expand Down
Binary file modified Main/Source/.vs/Effort.EF6/v15/Server/sqlite3/storage.ide
Binary file not shown.
Binary file modified Main/Source/.vs/Effort/v15/Server/sqlite3/storage.ide
Binary file not shown.
4 changes: 2 additions & 2 deletions Main/Source/Effort.Lab.EF6/Effort.Lab.EF6.csproj
Expand Up @@ -38,12 +38,12 @@
<HintPath>..\packages\EntityFramework.6.1.2\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="NMemory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a46696d54971e6d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Build\Packages\Library\.NET 4.5\Entity Framework 6\NMemory.dll</HintPath>
<HintPath>..\packages\NMemory.1.1.2\lib\net45\NMemory.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
1 change: 1 addition & 0 deletions Main/Source/Effort.Lab.EF6/packages.config
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.2" targetFramework="net45" />
<package id="NMemory" version="1.1.2" targetFramework="net45" />
</packages>
27 changes: 9 additions & 18 deletions Main/Source/Effort/DataLoaders/EntityTableDataLoaderFactory.cs
Expand Up @@ -22,14 +22,13 @@
// </copyright>
// --------------------------------------------------------------------------------------------


namespace Effort.DataLoaders
{
using System;
using System.Data.Common;
using System.Data.Entity;
#if !EFOLD
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Objects;
using Effort.Internal;
using System.Reflection;
#else
using System.Data.EntityClient;
Expand All @@ -44,19 +43,7 @@ public class EntityTableDataLoaderFactory : ITableDataLoaderFactory
private Func<EntityConnection> connectionFactory;
private EntityConnection connection;

private EntityConnection GetEntityConnection(Database data)
{
object internalContext = data.GetType().GetField("_internalContext", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(data);


MethodInfo getObjectContext = internalContext.GetType().GetMethod("GetObjectContextWithoutDatabaseInitialization", BindingFlags.Public | BindingFlags.Instance);

var objectContext = (ObjectContext)getObjectContext.Invoke(internalContext, null);

DbConnection entityConnection = objectContext.Connection;

return (EntityConnection)entityConnection;
}


/// <summary>
/// Initializes a new instance of the <see cref="EntityTableDataLoaderFactory" />
Expand All @@ -67,18 +54,22 @@ private EntityConnection GetEntityConnection(Database data)
/// </param>
public EntityTableDataLoaderFactory(Func<EntityConnection> connectionFactory)
{
var entityConnectionString_Fields = connectionFactory.Target.GetType().GetField("entityConnectionString", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
#if !EFOLD
var entityConnectionString_Fields = connectionFactory.Target.GetType().GetField("entityConnectionString", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

var entityConnectionString = entityConnectionString_Fields.GetValue(connectionFactory.Target);

if (entityConnectionString.Equals(""))
{
this.connectionFactory = () => GetEntityConnection(EntityFrameworkEffortManager.CreateFactoryContext(null).Database);
this.connectionFactory = () => EntityFrameworkEffortManager.CreateFactoryContext(null).Database.GetEntityConnection();
}
else
{
this.connectionFactory = connectionFactory;
}
#else
this.connectionFactory = connectionFactory;
#endif
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Main/Source/Effort/Effort.csproj
Expand Up @@ -259,6 +259,7 @@
<Compile Include="Internal\Common\ReflectionHelper.cs" />
<Compile Include="Internal\Common\TypeHelper.cs" />
<Compile Include="EntityConnectionFactory.cs" />
<Compile Include="Internal\Extensions\Database.GetEntityConnection.cs" />
<Compile Include="Internal\StorageSchema\CommonPropertyElementModifier.cs" />
<Compile Include="Internal\StorageSchema\ModificationFunctionMappingModifier.cs" />
<Compile Include="Internal\StorageSchema\ReturnTypeAttributeSelector.cs" />
Expand Down Expand Up @@ -360,4 +361,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
8 changes: 4 additions & 4 deletions Main/Source/Effort/EntityFrameworkEffortManager.cs
@@ -1,4 +1,5 @@
using System;
#if !EFOLD
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
Expand Down Expand Up @@ -32,9 +33,8 @@ internal static DbContext CreateFactoryContext(DbContext context)
}
}

throw new Exception(
"A working context must be provided using EntityFrameworkEffortManager.ContextFactory." +
" This setting is required for some features like IncludeGraph. Read more: http://entityframework-extensions.net/context-factory");
throw new Exception("The specified code require a ContextFactory to work. Example: EntityFrmeworkEffortManager.ContextFactory = (currentContext) => new EntitiesContext()");
}
}
}
#endif
@@ -0,0 +1,27 @@
#if !EFOLD

using System.Data.Entity;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Objects;
using System.Reflection;

namespace Effort.Internal
{
internal static class InternalExtensions
{
internal static EntityConnection GetEntityConnection(this Database database)
{
var internalContext = database.GetType().GetField("_internalContext", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(database);

var getObjectContext = internalContext.GetType().GetMethod("GetObjectContextWithoutDatabaseInitialization", BindingFlags.Public | BindingFlags.Instance);

var objectContext = (ObjectContext) getObjectContext.Invoke(internalContext, null);

var entityConnection = objectContext.Connection;

return (EntityConnection) entityConnection;
}
}
}

#endif
4 changes: 2 additions & 2 deletions Main/Source/Effort/Properties/AssemblyVersion.cs
Expand Up @@ -35,5 +35,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.3.2")]
[assembly: AssemblyInformationalVersion("1.3.2")]
[assembly: AssemblyFileVersion("1.3.3")]
[assembly: AssemblyInformationalVersion("1.3.3")]

0 comments on commit 45b0105

Please sign in to comment.