Skip to content

Commit 04d5a40

Browse files
Merge pull request #593 from johelvisguzman/issue592
(GH-592) Fixed issues with ef not being able to resolved the dbcontext type
2 parents 3e9b269 + 53ab7ed commit 04d5a40

File tree

4 files changed

+10
-95
lines changed

4 files changed

+10
-95
lines changed

src/DotNetToolkit.Repository.EntityFramework/Internal/EfRepositoryContextFactory.cs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class EfRepositoryContextFactory<TDbContext> : IRepositoryContextFactor
2424
#region Constructors
2525

2626
/// <summary>
27-
/// Initializes a new instance of the <see cref="EfRepositoryContextFactory{TDbContext}"/> class.
27+
/// Initializes a new instance of the <see cref="EfRepositoryContextFactory{TDbContext}"/> using an IOC container to resolve the <typeparamref name="TDbContext"/>.
2828
/// </summary>
2929
public EfRepositoryContextFactory() { }
3030

@@ -56,8 +56,6 @@ public EfRepositoryContextFactory(DbConnection existingConnection)
5656
/// <returns>The new repository context.</returns>
5757
public IRepositoryContext Create()
5858
{
59-
TDbContext underlyingContext;
60-
6159
var args = new List<object>();
6260

6361
if (_existingConnection != null)
@@ -70,37 +68,11 @@ public IRepositoryContext Create()
7068
args.Add(_nameOrConnectionString);
7169
}
7270

73-
try
74-
{
75-
underlyingContext = (TDbContext)Activator.CreateInstance(typeof(TDbContext), args.ToArray());
76-
}
77-
catch (Exception ex)
78-
{
79-
throw ex.InnerException ?? ex;
80-
}
71+
var underlyingContext = args.Count > 0
72+
? (TDbContext)Activator.CreateInstance(typeof(TDbContext), args.ToArray())
73+
: RepositoryDependencyResolver.Current.Resolve<TDbContext>();
8174

8275
return new EfRepositoryContext(underlyingContext);
83-
84-
}
85-
86-
#endregion
87-
}
88-
89-
/// <summary>
90-
/// An implementation of <see cref="IRepositoryContextFactory" /> with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
91-
/// </summary>
92-
/// <seealso cref="IRepositoryContextFactory" />
93-
internal class EfRepositoryContextFactory : IRepositoryContextFactory
94-
{
95-
#region Implementation of IRepositoryContextFactory
96-
97-
/// <summary>
98-
/// Create a new repository context.
99-
/// </summary>
100-
/// <returns>The new repository context.</returns>
101-
public IRepositoryContext Create()
102-
{
103-
return new EfRepositoryContext(RepositoryDependencyResolver.Current.Resolve<DbContext>());
10476
}
10577

10678
#endregion

src/DotNetToolkit.Repository.EntityFramework/RepositoryOptionsBuilderExtensions.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public static class RepositoryOptionsBuilderExtensions
1414
{
1515
/// <summary>
16-
/// Configures the context to use entity framework.
16+
/// Configures the context to use entity framework using an IOC container to resolve the <typeparamref name="TDbContext"/>.
1717
/// </summary>
1818
/// <param name="source">The repository options builder.</param>
1919
/// <returns>The same builder instance.</returns>
@@ -57,19 +57,5 @@ public static RepositoryOptionsBuilder UseEntityFramework<TDbContext>([NotNull]
5757

5858
return source;
5959
}
60-
61-
/// <summary>
62-
/// Configures the context to use entity framework with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
63-
/// </summary>
64-
/// <param name="source">The repository options builder.</param>
65-
/// <returns>The same builder instance.</returns>
66-
public static RepositoryOptionsBuilder UseEntityFramework([NotNull] this RepositoryOptionsBuilder source)
67-
{
68-
Guard.NotNull(source, nameof(source));
69-
70-
source.UseInternalContextFactory(new EfRepositoryContextFactory());
71-
72-
return source;
73-
}
7460
}
7561
}

src/DotNetToolkit.Repository.EntityFrameworkCore/Internal/EfCoreRepositoryContextFactory.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class EfCoreRepositoryContextFactory<TDbContext> : IRepositoryContextFa
2121
#region Constructors
2222

2323
/// <summary>
24-
/// Initializes a new instance of the <see cref="EfCoreRepositoryContextFactory{TDbContext}"/> class.
24+
/// Initializes a new instance of the <see cref="EfCoreRepositoryContextFactory{TDbContext}"/> using an IOC container to resolve the <typeparamref name="TDbContext"/>.
2525
/// </summary>
2626
public EfCoreRepositoryContextFactory() { }
2727

@@ -44,43 +44,14 @@ public EfCoreRepositoryContextFactory(DbContextOptions contextOptions)
4444
/// <returns>The new repository context.</returns>
4545
public IRepositoryContext Create()
4646
{
47-
TDbContext underlyingContext;
48-
49-
try
50-
{
51-
underlyingContext = _contextOptions != null
52-
? (TDbContext)Activator.CreateInstance(typeof(TDbContext), _contextOptions)
53-
: (TDbContext)Activator.CreateInstance(typeof(TDbContext));
54-
}
55-
catch (Exception ex)
56-
{
57-
throw ex.InnerException ?? ex;
58-
}
47+
var underlyingContext = _contextOptions != null
48+
? (TDbContext)Activator.CreateInstance(typeof(TDbContext), _contextOptions)
49+
: RepositoryDependencyResolver.Current.Resolve<TDbContext>();
5950

6051
return new EfCoreRepositoryContext(underlyingContext);
6152

6253
}
6354

6455
#endregion
6556
}
66-
67-
/// <summary>
68-
/// An implementation of <see cref="IRepositoryContextFactory" /> with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
69-
/// </summary>
70-
/// <seealso cref="IRepositoryContextFactory" />
71-
internal class EfCoreRepositoryContextFactory : IRepositoryContextFactory
72-
{
73-
#region Implementation of IRepositoryContextFactory
74-
75-
/// <summary>
76-
/// Create a new repository context.
77-
/// </summary>
78-
/// <returns>The new repository context.</returns>
79-
public IRepositoryContext Create()
80-
{
81-
return new EfCoreRepositoryContext(RepositoryDependencyResolver.Current.Resolve<DbContext>());
82-
}
83-
84-
#endregion
85-
}
8657
}

src/DotNetToolkit.Repository.EntityFrameworkCore/RepositoryOptionsBuilderExtensions.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public static class RepositoryOptionsBuilderExtensions
1414
{
1515
/// <summary>
16-
/// Configures the context to use entity framework core.
16+
/// Configures the context to use entity framework core using an IOC container to resolve the <typeparamref name="TDbContext"/>.
1717
/// </summary>
1818
/// <param name="source">The repository options builder.</param>
1919
/// <returns>The same builder instance.</returns>
@@ -59,19 +59,5 @@ public static RepositoryOptionsBuilder UseEntityFrameworkCore<TDbContext>([NotNu
5959

6060
return source;
6161
}
62-
63-
/// <summary>
64-
/// Configures the context to use entity framework with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
65-
/// </summary>
66-
/// <param name="source">The repository options builder.</param>
67-
/// <returns>The same builder instance.</returns>
68-
public static RepositoryOptionsBuilder UseEntityFrameworkCore([NotNull] this RepositoryOptionsBuilder source)
69-
{
70-
Guard.NotNull(source, nameof(source));
71-
72-
source.UseInternalContextFactory(new EfCoreRepositoryContextFactory());
73-
74-
return source;
75-
}
7662
}
7763
}

0 commit comments

Comments
 (0)