My WCF method tries to get some data from database. At the end calls this method:
public override IQueryable<Apartment> GetAll()
{
return base.GetAll().Include("Pictures").Include("Infos");
}
public virtual IQueryable<T> GetAll()
{
return DbSet;
}
but it seems error happens when it tries to open connection to database. I'm getting error The underlying provider failed on Open.". Can I somehow specify in WCF confg file whch are allowed database to connect or in my project allow WCF to connect to my database. Or there is some other solution?
public Base()
{
DataContext = new Context();
this.DbSet = DataContext.Set<T>();
DataContext.Configuration.ProxyCreationEnabled = false;
DataContext.Configuration.LazyLoadingEnabled = false;
}
...
public class Context:DbContext
{
public Context()
: base("DefaultConnection")
{
}
Create a class partial extension to Context. Within that override the constructor to pass in a different connection string (which also needs to be in the config as that name) to base such as
public partial class Context
{
public Context(string targetDBConnection) : base(targetDBConnection) {}
}
Related
I am looking to create a signalR Hub to get the SQL record updates in real time, using SQLDependency.
I used the EFCore Scaffolding database to create models as well as the DBContext, and using the repository pattern to work on retrieving data from the DB.
private Func<DBAContext> _contextFactory;
public Repository(Func<DBAContext> ContextFactory)
{
this._contextFactory = ContextFactory;
}
public someMethod()
{
using (var context = _contextFactory())
{
return context.Account.LastOrDefault();
}
}
Here's the issue: on invoking context, I get "Constructor on Type DBContext not found.
DBAContext.cs
public partial class DBAContext : DbContext
{
public DBAContext(DbContextOptions<DBAContext> options)
: base(options)
{
}
public virtual DbSet<Account> Account { get; set; }
}
.
.
.//OnConfiguring
.//Autogenerated onModelCreating
Here is where the error occurs:
public static void AddDbContextFactory<TDataContext>(this IServiceCollection services, string
connectionString) where TDataContext : DbContext
{
services.AddSingleton<Func<TDataContext>>((ctx) =>
{
var options = new DbContextOptionsBuilder()
.UseSqlServer(connectionString)
.Options;
return () => (TDataContext)Activator.CreateInstance(typeof(TDataContext), options);
});
}
}
Activator.CreateInstance is unable to resolve a constructor on the DBAContext class.
Startup.cs
services.AddDbContextFactory<DBAContext>
(Configuration.GetConnectionString("DefaultConnection"));
If anyone could explain me the issue, I'd really appreciate it
Thanks in Advance
I have a situation where I need to instantiate my DBContext after my solution has started up. I asked this question which indicated that I could do this with a constructor argument.
It was suggested that I implement as an example this:
var connection = #"Server=(localdb)\mssqllocaldb;Database=JobsLedgerDB;Trusted_Connection=True;ConnectRetryCount=0";
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlServer(connection);
using (var context = new BloggingContext(optionsBuilder.Options))
{
// do stuff
}
However I have implemented the repository pattern (for better or worst) and given my changed circumstances - not having a connection string until after the solution has run startup - I need to implement this into the base repository class and I am at a bit of a loss..
Currently I have this:
public class EntityBaseRepository<T> : IEntityBaseRepository<T> where T : class, IEntityBase, new()
{
public JobsLedgerAPIContext _context;
#region Properties
public EntityBaseRepository(JobsLedgerAPIContext context)
{
_context = context;
}
#endregion
public virtual IQueryable<T> GetAll()
{
return _context.Set<T>().AsQueryable();
}
public virtual int Count()
{
return _context.Set<T>().Count();
}
......
How do I implement this change both instantiating the DBContext in the constructor (there by bypassing the need to add the context as a service in startup) and then with the wrapping each of the virtual methods with "using" etc
EDIT.. Camilo indicated I had not identified when I have the database name.
The basic situation is that the system starts up (This is an Aurelia SPA project which is irrelevant to this issue) sends the package to the browser which shows a login screen. User logs in.. User is verified via a JWT controller.. Once verified in the controller (using a catalog database that has one table with 3 fields - username, password, database name) I use the database name to create a connection string and then instantiate my DBContext at that point.. so via a constructor.
The answers below need to be modified as the one with the factory answer (promising) has errors as discovered by this question.. Nkosi responded with an great answer to the error.
EDIT 2..
This is a response to the edited question below:
Here is my original Client Repository with :base(context) on the constructor.
using JobsLedger.DATA.Abstract;
using JobsLedger.MODEL.Entities;
namespace JobsLedger.DATA.Repositories
{
public class ClientRepository : EntityBaseRepository<Client>, IClientRepository
{
private new JobsLedgerAPIContext _context;
public ClientRepository(JobsLedgerAPIContext context) : base(context)
{
_context = context;
}
public void RelatedSuburbEntities(Suburb _suburb)
{
_context.Entry(_suburb).Reference<State>(a => a.State).Load();
}
}
}
It has a reference to the base class "context". I am not sure how to modify this given that I believe I still need that ":base(context)" at the end. As well, I have a method in this that accesses _context as well which is part of the constructor...
Further I assume that I can no longer inject the service into the controller but instead new it up once I have secured the connection string and then pass that connection string to service.
Also, Given I have now added a singleton on the startup do I need to remove the original entry? :
services.AddDbContext<JobsLedgerAPIContext>(options => options.
UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("JobsLedger.API")));
effectively replacing it with my singleton reference as per below:
services.AddSingleton(typeof(IContextFactory<>), typeof(ContextFactory<>));
Edited
The answer has been edited to rectify the mistake spotted and
fixed by Nkosi. Thanks, #Nkosi.
Implement a factory pattern. You can create a factory, call it ContextFactory as below:
First, define the interface. Further modified, removed the connectionString parameter
public interface IContextFactory<T> where T : DbContext
{
T CreateDbContext();
}
Create a factory class that implements this interface (edited as per Nkosi answer). Further modified to inject IHttpContextAccessor
public class ContextFactory<T> : IContextFactory<T> where T : DbContext
{
private readonly HttpContext _httpContext;
public ContextFactory(IHttpContextAccessor contextAccessor)
{
_httpContext = contextAccessor.HttpContext;
}
public T CreateDbContext()
{
// retreive the connectionString from the _httpContext.Items
// this is saved in the controller action method
var connectionString = (string)_httpContext.Items["connection-string"];
var optionsBuilder = new DbContextOptionsBuilder<T>();
optionsBuilder.UseSqlServer(connectionString);
return (T)Activator.CreateInstance(typeof(T), optionsBuilder.Options);
}
}
Then modify your base repository and make the JobsLedgerAPIContext protected. This context is going to be set by the derived class. Further modified to remove the constructor. It will use the parameterless constructor.
public class EntityBaseRepository<T> : IEntityBaseRepository<T> where T : class, IEntityBase, new()
{
protected JobsLedgerApiContext Context { get; set; }
public virtual IQueryable<T> GetAll()
{
return Context.Set<T>().AsQueryable();
}
public virtual int Count()
{
return Context.Set<T>().Count();
}
}
Change your derived class to use IContextFactory. Further modified to use the _contextFactory.CreateDbContext() parameter less method
The IClientRepository should have SetContext method defined.
public class ClientRepository : EntityBaseRepository<Client>, IClientRepository
{
private readonly IContextFactory<JobsLedgerApiContext> _contextFactory;
public ClientRepository(IContextFactory<JobsLedgerApiContext> factory)
{
_contextFactory = factory;
}
// this method will set the protected Context property using the context
// created by the factory
public void SetContext()
{
Context = _contextFactory.CreateDbContext();
}
public void RelatedSuburbEntities(Suburb suburb)
{
Context.Entry(suburb).Reference<State>(a => a.State).Load();
}
}
In the controller, that receives IClientRepository instance, you can set the connection in the HttpContext.Items, which will be valid for the request. This value will then be retrieved by the ContextFactory using IHttpContextAccessor. Then you simply call the _repository.SetContext(); method on the repository.
public class HomeController : Controller
{
private readonly IClientRepository _repository;
public HomeController(IClientRepository repository)
{
_repository = repository;
}
public IActionResult Index()
{
// save the connectionString in the HttpContext.Items
HttpContext.Items["connection-string"] = "test-connection";
// set the context
_repository.SetContext();
return View();
}
}
Make sure you register the IContextFactory in ConfigureServices as open generics and Singleton as below, also register the HttpContextAccessor and IClientRepository
services.AddHttpContextAccessor();
services.AddSingleton(typeof(IContextFactory<>), typeof(ContextFactory<>));
services.AddTransient<IClientRepository, ClientRepository>();
You may define your JobsLedgerAPIContext like this:
public class JobsLedgerAPIContext : DbContext
{
// public DbSet<Job> Jobs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=dotnetcore;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// may need to reflect entity classes and register them here.
base.OnModelCreating(modelBuilder);
}
}
I'm developing an ASP.NET 4.6.2 Web App with EF 6. The backend is a PostgreSQL database in several schemas : the tables and views are exactly the same in every schema. Depending on what the users do, I must connect specifically to one schema or another.
That works fine by overriding the OnModelCreating method but only once.
Should the user want to change his environment, I can instanciate the context with the right parameter but it doesn't call the OnModelCreating method. The context is kept somewhere and connects to the previous schema.
-> How can I invalidate the context or force the switch to the other schema ?
Here's my code :
public partial class MyDB : DbContext
{
public Instances Instance { get; set; }
public string Schema
{
get
{
switch(Instance)
{
case Instances.I1:
return "schema1";
case Instances.I2:
return "schema2";
case Instances.I3:
return "schema3";
case Instances.I4:
return "schema4";
case Instances.I5:
return "schema5";
default:
return "public";
}
}
}
public MyDB(bool Proxy, Instances Instance)
: base("MyDB")
{
this.Configuration.ProxyCreationEnabled = Proxy;
this.Instance = Instance;
this.Database.Log = s => System.Diagnostics.Debug.Print(s);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema(Schema);
modelBuilder.Conventions.Add(new FunctionsConvention<MyDB>(Schema));
}
}
class NpgsqlConfiguration : DbConfiguration
{
public NpgsqlConfiguration()
{
SetModelCacheKey(ctx => new EntityModelCacheKey((ctx.GetType().FullName + ctx.Database.Connection.ConnectionString).GetHashCode()));
SetProviderServices("Npgsql", Npgsql.NpgsqlServices.Instance);
SetProviderFactory("Npgsql", Npgsql.NpgsqlFactory.Instance);
SetDefaultConnectionFactory(new Npgsql.NpgsqlConnectionFactory());
}
}
Thanks for your help !
Its Simple use two connection strings
MyDB db;
if (user.Type == 1)
{
db = new MyDb("nameOfConnectionString1");
}
else
{
db = new MyDb("nameOfConnectionString2");
}
you may need to create overload for constructor
public MyDB(string conStr, bool Proxy, Instances Instance)
: base(conStr)
In this project I am trying to migrate from .NET to .NET Core. Here I have a code that I want to implement in .NET Core.
public partial class CompanyFormsContext : DbContext
{
public CompanyFormsContext()
: base("name=CompanyFormsContext")
{
}
public CompanyFormsContext(string connName)
: base("name=" + connName)
{
}
...
}
In .NET Core, string is not accepted as a parameter to DbContext. Instead, you can pass DbContextOptions as a parameter. For example, in the following link: http://ef.readthedocs.io/en/latest/miscellaneous/configuring-dbcontext.html
You can see the example:
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
}
and
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("Filename=./blog.db");
using (var context = new BloggingContext(optionsBuilder.Options))
{
// do stuff
}
what I want to do is similar to this. I could possibly create an instance of DbContextOptions and pass it but I do not know how I would modify the SqlServer connection as I am passing it through the base class constructor.
The most important thing that I cannot figure out is that I want to be able to keep my empty constructor which would trigger a default "CompanyFormsContext" connection. It would also be great if I could simply change the connection name while passing it as a parameter in the CompanyFormsContext constructors.
I was thinking of the following as an alternative way instead of the base constructors but I would prefer to keep the base constructor functionality.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (connName == null)
{
optionsBuilder.UseSqlServer(#"Server=.\;Integrated Security=True;Database=CompanyFormsContext");
}
else
{
optionsBuilder.UseSqlServer(#"Server=.\;Integrated Security=True;Database=" + connName);
}
}
You could create a static method which returns a DbContextOptions<BloggingContext> which will be created from a passed connection string.
So your class could look something like this:
public partial class CompanyFormsContext : DbContext
{
public CompanyFormsContext()
: base(CreateOptions(null))
{
}
public CompanyFormsContext(string connName)
: base(CreateOptions(connName))
{
}
private static DbContextOptions<BloggingContext> CreateOptions(string connName)
{
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("Filename=./blog.db");
if (connName == null)
{
optionsBuilder.UseSqlServer(#"Server=.\;Integrated Security=True;Database=CompanyFormsContext");
}
else
{
optionsBuilder.UseSqlServer(#"Server=.\;Integrated Security=True;Database=" + connName);
}
return optionsBuilder.Options;
}
I'm trying to register IDbContext in Unity but I'm getting this
error
"The type IDbContext does not have an accessible constructor."
UnityConfig.cs
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IProductServices, ProductServices>();
container.RegisterType(typeof(IRepository<>), typeof(Repository<>));
//---all ERROR below---
//container.RegisterType(typeof(IDbContext), typeof(DbContext));
//container.RegisterType<IDbContext>();
var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationContext"].ConnectionString;
container.RegisterType(typeof(IDbContext), typeof(DbContext), new ContainerControlledLifetimeManager(), new InjectionConstructor(connectionString));
}
IDbContext.cs
public interface IDbContext
{
IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;
int SaveChanges();
}
ApplicationContext.cs
public partial class ApplicationContext : DbContext, IDbContext
{
public ApplicationContext()
: this("name=ApplicationContext")
{
}
public ApplicationContext(string name)
: base(name)
{
}
public DbSet<Product> Products { get; set; }
public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
{
return base.Set<TEntity>();
}
}
You have 2 constructor in your DbContext therefore Unity can not initialize it simply remove ApplicationContext(string name) constructor an let default exist.
public partial class ApplicationContext : DbContext, IDbContext
{
public ApplicationContext()
: this("name=ApplicationContext")
{
}
public DbSet<Product> Products { get; set; }
public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
{
return base.Set<TEntity>();
}
}
An in unity side write:
container.RegisterType<IDbContext, ApplicationContext>();
But if you want keep your constructors try this:
container.RegisterType<IDbContext, ApplicationContext>(
new InjectionConstructor("YourConnectionStringYouProvidedSomewhere"));
So you could do something like this:
public class MyContext : DbContext
{
public MyContext() : base("connectionstringproperty") { }
}
public interface IMyContextFactory : IDbContextFactory<MyContext>
{
}
public class MyContextFactory : IMyContextFactory
{
public MyContext Create()
{
return new MyContext();
}
}
Register Like this:
container.RegisterType<IMyContextFactory, MyContextFactory>(new PerResolveLifetime());
And use in class like so:
public class UserService
{
IMyContextFactory contextFactory
public UserService(IMyContextFactory contextFactory)
{
contextFactory = contextFactory;
}
public List<User> GetUsers()
{
using(var context = this.contextFactory.Create())
{
return context.Users.ToList();
}
}
}
I don't use repositories until I really have to (which is nearly never :) ). But if UserService was a repository class instead (it kinda is at the moment) the implementation would be the same using constructor injection.
DbContext
If you want testability, EF 6 did a good job in making all methods and properties virtual so you could override them rather than creating an interface to back it.
For dependency injection, EF created IDbContextFactory<T> where T : DbContext
You should be inheriting from DbContext so that you can create some tables in the database. When you inherit you can create different constructors depending on your needs, the easiest I find is to create a parameterless one and call the base constructor overload which takes the web.config connection string property.
DbContext does not have a public, default constructor. The most minimial public constructor is one which takes a string value containing the name of the connection string to use (defined in your app.config or web.config file) or the actual connection string to the database itself.
If you have such a connection string defined in your app.config or web.config (or you have a suitable default connection string to be used throughout your application), try the following Unity registration:
// In the code below, "connection_string" is either the full database
// connection string or the name of the connection string as defined
// in the app.config or web.config files
//
// NOTE: This method requires a LifetimeManager.
// I used the default "singleton container" provided by unity.
// This may not be appropriate for your application--please use an
// appropriate container lifetime manager.
container.RegisterType(typeof(IDbContext), typeof(DbContext),
new ContainerControlledLifetimeManager(),
new InjectionConstructor("connection_string"));
If you have multiple databases you will need to connect to, then you may want to subclass DbContext (e.g. MyDB1DbContext, MyDB2DbContext) or use named registrations. If you subclass DbContext, then you could have the subclasses implement a public default constructor, passing a suitable connection string to the base DbContext class--at which point, your context registrations become simpler.
For one application-wide DbContext though, I'd probably just use the code snippet above.
Update after OP's Update
Unless you'll need to use multiple connection strings with your application-defined DbContext, I would simply define as follows:
public class ApplicationDbContext() : System.Data.Entity.DbContext
{
public ApplicationDbContext() : base("my_connection_string_name")
{ }
... // Rest of context class definition
}
Then, to register:
// NOTE: Can also use the generic version--may need to also change the
// lifetime management of the context depending on your application.
container.Register(typeof(IDbContext), typeof(ApplicationDbContext),
new ContainerControlledLifetimeManager());