Skip to content

Commit 9a4d602

Browse files
committed
dbContextType for EfRepository is a required parameter if you don't use dependency resolver
1 parent 52dda95 commit 9a4d602

3 files changed

Lines changed: 29 additions & 36 deletions

File tree

SharpRepository.EfRepository/EfConfigRepositoryFactory.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,31 @@ private DbContext GetDbContext()
4848
dbContextType = Type.GetType(dbContextTypeString);
4949
}
5050

51+
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
52+
DbContext dbContext = null;
53+
54+
// the default way of getting a DbContext if there is no Ioc container setup
55+
if (dbContextType != null)
56+
{
57+
dbContext = String.IsNullOrEmpty(connectionString) ?
58+
(DbContext)Activator.CreateInstance(dbContextType) :
59+
(DbContext)Activator.CreateInstance(dbContextType, connectionString);
60+
61+
return dbContext;
62+
}
63+
5164
// check for required parameters
52-
if (RepositoryDependencyResolver.Current == null && (String.IsNullOrEmpty(connectionString) || dbContextType == null))
65+
if (RepositoryDependencyResolver.Current == null)
5366
{
5467
throw new ConfigurationErrorsException("The connectionString and dbContextType attribute are required in order to use the EfRepository via the configuration file, unless you set the RepositoryDependencyResolver to use an Ioc container.");
5568
}
56-
57-
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
58-
DbContext dbContext = null;
59-
60-
// if there is an IOC dependency resolver configured then use that one to get the DbContext, this will allow sharing of context across multiple repositories if the IOC is configured that way
61-
if (RepositoryDependencyResolver.Current != null)
69+
else
6270
{
63-
try
64-
{
65-
dbContext = dbContextType == null
71+
// if there is an IOC dependency resolver configured then use that one to get the DbContext, this will allow sharing of context across multiple repositories if the IOC is configured that way
72+
return dbContextType == null
6673
? RepositoryDependencyResolver.Current.Resolve<DbContext>()
6774
: (DbContext)RepositoryDependencyResolver.Current.Resolve(dbContextType);
68-
69-
if (dbContext != null)
70-
{
71-
return dbContext;
72-
}
73-
} catch (Exception)
74-
{}
7575
}
76-
77-
// the default way of getting a DbContext if there is no Ioc container setup
78-
dbContext = dbContextType == null
79-
? new DbContext(connectionString)
80-
: (DbContext)Activator.CreateInstance(dbContextType, connectionString);
81-
82-
return dbContext;
8376
}
8477
}
8578
}

SharpRepository.Samples.MVC5/Global.asax.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ protected void Application_Start()
2020
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
2121
RouteConfig.RegisterRoutes(RouteTable.Routes);
2222
BundleConfig.RegisterBundles(BundleTable.Bundles);
23-
MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository", "efConnectionString"); // holds connection string on repository.json
24-
// MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository"); // no connection string on repository.json
23+
// MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository", "efConnectionString"); // holds connection string on repository.json
24+
MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository"); // no connection string on repository.json
2525
}
2626
}
2727

2828
/// <summary>
2929
/// Registers ContactsDbContext, that knows the connection string, as DbContext
3030
/// </summary>
31-
//public class DbContexRegistry : Registry
32-
//{
33-
// public DbContexRegistry()
34-
// {
35-
// For<DbContext>().Use(new ContactsDbContext());
36-
// }
37-
//}
31+
public class DbContexRegistry : Registry
32+
{
33+
public DbContexRegistry()
34+
{
35+
For<DbContext>().Use(new ContactsDbContext("name=ContactsDbContext"));
36+
}
37+
}
3838
}

SharpRepository.Samples.MVC5/Models/ContactsDbContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ namespace SharpRepository.Samples.MVC5.Models
44
{
55
public class ContactsDbContext : DbContext
66
{
7-
public ContactsDbContext()
8-
: base("name=ContactsDbContext")
7+
public ContactsDbContext(string connectionString) : base(connectionString)
98
{
9+
1010
}
11-
11+
1212
public virtual DbSet<Contact> Contacts { get; set; }
1313
}
1414
}

0 commit comments

Comments
 (0)