How/Where I do to override the app.config ConnectionString at runtime? - wpf

I'm using Entity Framework 4.1 Code First, Sqlite and WPF.
I want to redefine the app.config ConnectionString to sqlite, so I can define the Data Source (path for Sqlite database file) at runtime.
My guess is using the EntityConnectionStringBuilder to create the connection string. But I don't know what the event where I build and assign the connection string so the EF Code First will detect the change and will use the newer ConnectionString in all the code for the application (instead to use the app.config ConnectionString). I imagine that it's to be placed on Application_Startup event, but I don't know if this is the best pratice.
Thank you in advance.

If that's what you're after,
You can pass the string at runtime via DbContext(string) constructor - see here for more DbContext(string)
You could also make your context implementation take that param and pass it to the base DbContext.
hope this helps

IF you are using EF 4.1 CF, then once you do your *.edmx file generation it should create your app.config automatically. You don't need to create your own app.config or change it if you have an existing one.

Related

Best way to store data in wpf app

I just want to know about the best way to store simple data in wfp application. I'm using SQL CE, but sometimes I need to store simple data (for instance one string). I don't want to use for it db, and I think it's the right decision.
Could you please give me a guidance on how should I do that in best way or any useful resource?
Is there any particular features data settings storage for pattern MVVM?
Thank You.
Use Settings for exactly this scenario. It's simple, allows per user and system wide scenarios and is build into Visual Studio nicely.
Double clicking on Settings.settings in the Properties folder of your project (in Solution Explorer) brings up the editor and you can access these in code with something simple like Properties.Settings.Default.StringPropertyX.
http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
if you're going to be upgrading in the future, I suggest that you create a small abstraction layer that you can upgrade to using any DB or other source, when you need to.
Until you do the upgrade, I would suggest a simple XML file using XDocument and XElement (and the rest of that api).
The reason why I am suggesting this is because you can use Linq on them easily and this enables you to change the provider in the future without any problems.
here is the MSDN on XDocument http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx .
I hope this proves to be helpful to you ;-)
Cheers,
Max
p.s. lemme know if this is what you needed or not!
If you publish the application through ClickOnce then you can use IsolatedStorage. It is pretty easy to use, just a few lines of code. You can learn how to use it on WPF isolated storage.
If you don't publish it through ClickOnce then I would suggest you keep it simple and use static class.
you can use file system.. try to create xml file or csv file in the wpf app directory...
you can read and write to same file.. so that you can save what ever you want to that file and read from the same file..

Can I use the connection from Entity Framework or should I create a new one?

I have a SQLite Database that I access using System.Data.SQLite. The database is read into my Entity Framework Objectcontext. However, I also need direct access to the database in certain cases.
Can I / Should I use the same connection that my Entity Framework object context is using? I.e. ObjectContext.Connection?
If yes, how can I access it? Casting Entities.Connection to SQLiteConnection results in "Unable to cast object of type 'System.Data.EntityClient.EntityConnection' to type 'System.Data.SQLite.SQLiteConnection'."
If not, should I create a seperate connection, open it and keep it open for the application duration, or should I open and close the connection every time I want to access the database with it?
ObjectContext.Connection is an EntityConnection. You can get the store connection via:
var sc = ((EntityConnection)ObjectContext.Connection).StoreConnection;
You can cast that to the SQLiteConnection.
It's OK to use this connection if you need it.
But, as #Chris mentioned, you can just use ObjectContext.ExecuteStoreQuery, etc., if that will do for you.
If you're using the DBContext API you can actually execute a direct query by using this: aDBContext.Database.SqlQuery
I wouldn't be surprised if something similar exists for the ObjectContext API, but I barely used that one and thus can't tell you what it is.
Now if for some reason you can't (or don't want) to do that, I'd create a new connection for your custom queries. Put them in a Using and close it as soon as you're done with it.
Entity Framework has built in methods for executing raw SQL queries if that's what you need to do.

Winforms ConnectionString and TeamCity

We are starting a new WinForms project and decided to use TeamCity to create builds and run unit and integration tests. The project deals with database. We have 3 databases (developDB (this is used by developers while developing =) ), testDB (this is used by teamcity to run tests) and productionDB(this is used by client)). TeamCity has 3 buildConfiguration. The first is triggered when commit happens. The second is triggered every night to run integration tests. And the third is triggered by developer when we what to make a release. So I want TeamCity to be able to change connectionString depending on what kind of build happens. Also I don't want to store connectionString in app.config (I don't want client to know the user and password). What options are available to perform the task?
Thanks in advance!
Updated
I use NHibernate and FluentNHibernate to connect to databases if it matters.
In this situation, I would use TeamCity to run a nant script to perform the build.
NAnt allows you to modify config file values (such as your connection string) at build time.
An example of using TeamCity/NAnt to deploy to different staging environments can be found at this blog post:
http://thecodedecanter.wordpress.com/2010/03/25/one-click-website-deployment-using-teamcity-nant-git-and-powershell/
As #surfen suggests, the connection string values for each environment should be encrypted to prevent credentials from being stored in plain text.
I have not used TeamCity, but I have written multiple applications with dynamically changing ConnectionStrings during logon process (ie. at runtime), and It's quite simple.
You didn't tell how do you connect to your Database. Since you mention app.config, I suppose it is ADO.NET DataSets or simmilar technology, which creates a read-only(getter) ConnectionString in your Settings.Designer.cs / app.config.
What I did, was to create a setter method in Settings.cs (not Settings.Designer.cs) for the ConnectionString property like this:
public void setNorthwindConnectionString(String value) {
this["NorthwindConnectionString"] = value;
}
My generated DataSet then uses this NorthwindConnectionString for accessing data.
You can use preprocessor directives for conditional setup of your ConnectionString:
#if DEBUG
Console.WriteLine("Mode=Debug");
Settings.Default.setNorthwindConnectionString("(DebugDBConnectionString)");
#else
Console.WriteLine("Mode=Release");
Settings.Default.setNorthwindConnectionString("(ReleaseDBConnectionString)");
#endif
You could also encrypt your connection strings, and copy the right app.config during post build event.
I am assuming you would be using msbuild to build your projects in Team city. If that is the case, then you can send the Conditional Compilation Symbol where in you can pass what ever symbols you need.
Once you have the symols, you can do things like:
#if DEVBUILD
//.... Your Connection String Code here
#endif
#if INTBUILD
.... Your Connection String Code here
#endif
That's the answer to your frst question.
Looking at the second part of your question, where in you do not want to store the user name & password in the app.config,
Options:
try intergrated security, it will use your domain account
if option cannot be used, try keeping your connection string as a Registry Key, so that its not obvious or an Environment variable.

How do create a custom database driver for CodeIgniter

How do you create a custom database driver to extend CodeIgniter's functionalities to other types of database systems? I'm using iRODS (www.irods.org). I have a version of the site created using MySQL, but I want to be able to change the database backend with minimal changes. Is there an easy way to add this function, like how you can add a custom library in CI? I haven't been able to find any so far.
I'm assuming you mean how do you create a custom Active Record driver for codeigniter? Otherwise I'm probably far off the mark here but:
There is no way I know of to simply extend or override the DB classes it is not a common thing. You can implement your own and patch up your CI config to use the new DB though.
Under system/database/drivers you find all the AR driver source. You would need to reimplement each function in each of the four files (may be able to skip on forge if you don't use it.)
I'd use the MySQL driver as a starting template as you mention you already use that, in which case you'll want to make sure all the features you use are re-implemented.
It sounds like a daunting task if you're not too experienced but I assure you the code is pretty simple.

Using a test/dev/prod database strategy with Entity Framework

When working in Ruby (specifically, in Rails), I can automatically run my tests using a testing database, and also choose easily between a development or production database. Still new to Entity Framework (WPF), but it seems less than simple.
My Entity assembly has an App.Config file that holds a reference to the database, and I need to copy this App.Config file to all runnable projects (e.g. questions 1113361 and 2233897).
If I use Test->Database Test Configuration without explicitly copying App.Config from my EF project, I get
System.ArgumentException: The
specified named connection is either
not found in the configuration, not
intended to be used with the
EntityClient provider, or not valid.
which is the same as if I have a missing or different App.Config file.
Strangely even if I do change all the connection stings in my Entity project and testing project to the same database, I still get that error.
Is there a step I'm missing when telling Visual Studio (2010 Ultimate) that I want my tests to run using a different testing database, or is this just not supported with EF 4?
Also, Is there some way to change database context other than copying App.Config files back and forth? Seems like a serious way to ignore separation of concerns if not, so I think I'm missing something.
Strangely, as I was playing around after doing this, it started working. Somebody might comment on this, but it looks like I was using Test->Database Test Configuration at the wrong time.
To wit:
Clean project, no App.Config, use Test->Database Test Configuration to set the test database. It generates an app.config (lowercase, notably). You get an error. Even if you check all the connection strings (weird)
Clean project, first copy the App.Config from from you Entity project. Now when you use Test->Database Test Configuration, it will populate the App.Config that you copied, and it works.
I imagine it was some issue I was creating when copying the connection strings.
I've also confirmed that this works with my Entity project using a "Main" database while my testing project uses a "Test" database.
Wonder if someone can confirm/clarify this?

Resources