In my web application project I am using MYSQLMemberShipProvider. Now I want that instead of reading the connection string from web.config file, it will read the connection string from external file every time.
So that I am implementing the custom membership provider class, this class inherits the MembershipProvider class.
But the problem is that if I inherits the MembershipProvider class then I have to implement all of its method in my custom membership provider class, But I want to use all other inbuilt methods of Memebership. What can I do.
I only want to add the code like below:
public class CustomSqlMembershipProvider :MembershipProvider
{
public override void Initialize(string name, NameValueCollection configs)
{
base.Initialize(name, configs);
Connectionstring objProducts = // reading the connection string.
}
}
But on compilation it is giving me the error does not implement inherit abstract member.
Please Suggest me any idea.
Thanks in advance
Aayushi
What are your motivations for reading in the connection string from another file, is it purely to get a different connection string for debug/release environments? If that is the case you could use web.config transformations if you have VS2010. It is a very clean solution that may be of use to you.
Related
Is it possible to create a code-first Entity Framework model that connects to an existing database using ODP.Net without having any settings in the app.config file?
I have tried many different things.
Currently I am setting DbConfiguration:
sealed class EntityFrameworkConfiguration : DbConfiguration
{
public static readonly DbConfiguration Instance = new EntityFrameworkConfiguration();
EntityFrameworkConfiguration()
{
this.SetDefaultConnectionFactory(new OracleConnectionFactory());
this.SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance);
}
}
DbConfiguration.SetConfiguration(EntityFrameworkConfiguration.Instance);
I am passing an OracleConnection directly into the EF context.
However, I either have problems with the SQL being generated in SQL Server format (using double-quotes around table aliases), or I get the following error:
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
Additional information: Unable to determine the provider name for provider factory of type 'Oracle.ManagedDataAccess.Client.OracleClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
Has anyone any experience of getting this to work without polluting app.config with crud?
Yes. To complete the switch from machine.config/app.config to code-based configuration, I had to also include a call to SetProviderFactory().
public sealed class EntityFrameworkConfiguration : DbConfiguration
{
public static readonly DbConfiguration Instance = new EntityFrameworkConfiguration();
public EntityFrameworkConfiguration()
{
SetDefaultConnectionFactory(new OracleConnectionFactory());
SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance);
SetProviderFactory("Oracle.ManagedDataAccess.Client", new OracleClientFactory());
}
}
I also called DbConfiguration.SetConfiguration(EntityFrameworkConfiguration.Instance); in the startup of my application because I had DbContext's in multiple assemblies that all needed to share this configuration.
On a side note, I have also found this to be effective in allowing your application to work around the ConfigurationErrorsException: The 'DbProviderFactories' section can only appear once per config file for cases where you may not have access to repair the user's machine.config.
Uff. Found the problem.
Because I was registering column mapping using lower case the query didn't work. The column and table names must be in upper-case.
How silly.
I have a class library that is referenced from a web application. The class library defaults to using TestProject.Properties.Settings.Default.NFU_Custom_Website_DataConnectionString1. I would like it to get the connectionstring from ConfigurationManager.ConnectionStrings.
I can't change the .designer.cs file because it will get overwritten. If possible I would like to avoid creating a class which inherits from the .dbml file and setting the connection string in there. My boss recommends creating a web application project and deleting all of the default.aspx etc files from it and using that instead of a class library, is this a viable solution?
Thanks
Joe
You can do that. We had done that inadvertently and I got this problem when I switched to a class library. Rather than switch back I found the answer here: Point connectionstring in dbml to app.config
In summary:
Set the connection property of the DBML to (none) in the designer. This will remove the default constructor from the DB Context class.
Create a partial class for your DB Context with a default constructor:
using System.Configuration;
namespace TestProject
{
public partial class MyDBContext
{
public MyDBContext() : base(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString)
{
OnCreated();
}
}
}
I have a solution with multiple project using the same domain model. I thus created a class library that holds my domain models. This class library also contains other parameters that are used within my projects. I then add the reference to the class library in each of my projects.
My class library also has some repository classes derived from this example.
I however have an issue with connecting to a database. I want my class library to be able to connect to the database since I defined my database context class in there, where I set my database sets. With a single project, I usually define my connection string in my web.config file. But the class library has no web.config file. How do I set my connection string?
EDIT
Say i have the constructor of my database context, mydbcontext, defined in the class library as
public mydbcontext() : base(ConfigurationManager.ConnectionStrings["DatabaseCon"].ConnectionString)
{
}
If I understanding this right, will it be OK to just set the name of the connection string of each project to "DatabaseCon"?
You don't.
Pass in the connection string as a dependency to whatever classes that require it.
You can encapsulate the access to it - but you should instantiate it in whatever program that uses this library. This program will hold the connection string in its configuration.
You should define the connection string in the web.config file of the application that is using the class library. As an alternative you could hardcode the connection string into the constructor of your DbConext inside the class library - pretty bad approach because you won't be able to modify it from the outside - for example you will have hard time managing different connection strings for the different environments - staging, production, ...
I'm trying to get myself familiar with MVC3 and autofac but I've encountered small problem that I'm having trouble resolving.
I am using autofac integrated with MVC3 and all works well, pages are loading correctly, dependencies are being injected and that's cool. What's bugging me is how to use autofac's Container or MVC's DependencyResover in class library project.
I'm trying to create static class that will help me handle domain events. I simply want to be able to call the method with event parameter and everything should be handeled by this class. Here is code:
public static IContainer Container { get; set; }
public static void Raise<T>(T e) where T : IDomainEvent
{
foreach (var eventHandler in DomainEventManager.Container.Resolve<IEnumerable<EventHandlers.Handles<T>>>())
{
eventHandler.Handle(e);
}
}
As you can see it's pretty straightforward and everything would work great if it wasn't MVC approach. Some of my dependencies are registeres as InstancePerHttpRequest (NHibernate' session), while other are registered as InstancePerDependency or SingleInstance. Thus when I try to use container created in my UI project, I get exception that there is no httpRequest tag available.
How can i reuse the Container created in web project to get access to all of it's features, including InstancePerHttpRequest and httpRequest tag?
Or maybe there is other solution to my problem? I was thinking about using delegate function to obtain event handlers, but I cannot (can I?) create generic delegate that I would not need to initialize with concrete type at time of assignment.
Why I want to do this using static class is basically every entity and aggregate or service needs to be able to raise domain event. Injecting EventManager into every one of these would be troublesome and static class is exactly what would resolve all my problems.
If anyone could help me get my head around it I would be grateful.
Cheers, Pako
You shouldn't be referencing your container directly from your app code. This looks like the Service Locator anti-pattern. The correct action is to pass your objects the services they need to do their jobs, usually done through constructor parameters. BUT... if you are going to insist on depending on a global static, then at least model EventManager as a singleton, such that usage would look like:
EventManager.Current.Raise<SomeEvent>(someObject);
and then you can set EventManager.Current equal to a properly constructed instance when your app is initialized.
I would like to write and read data to Session even if my class not inherited from controller or helper. Something like this:
public class User
{
public void CreateSession()
{
Session["key"]=data;
}
public void ReadSession()
{
data=(string)Session["key"];
}
}
Important thing - I need to get instance of User class in some actions
and views. Am I be able to inherit User class from controller or
helper. Because I've already try that, but I got some errors.
How can I achive this?
Thanks in advance.
HttpContext.Session will work, it's a static available anywhere to an assembly that references System.Web.
If your code is in your main website assembly, this should work well. If it's not, you'll have to create a dependency on System.Web which you may or may not want to do.
You should do these kind of things in your controller.
You can access HttpContext directly, but that's not "the ASP.NET MVC way", and you loose some of the advantages of using MVC in the first place. Like making testing harder, and directly coupling your application to HttpContext...
Also, if it's a new session, or Session["key"] is not set for some reason, data=(string)Session["key"]; would throw a null reference exception.