Subsonic SQLite Multiple Files - sqlite

I have an application that must be accessed for many users.
To optimize the performance I intend to store each user profile information at a independant database file.
I need everytime a user login the application, to setup a new provider linked with his own database.
All databases have the same structure. So while querying user the commom generated DAL classes must switch for the database file relative the the user.
Is there a way for configure SubSonic for doing that switch at runtime?
Thanks.

Well, assuming we 're talking about SubSonic3:
I have made a patch for this and logged it as an issue in the SubSonic Templates project on github, where the source is available. You can find the issue (and a link to the code) here.
After you apply the patch, you will have a new DefaultDataProvider property which does exactly what you want. Use it like this (e.g. after a user logs in):
YourSubSonicGeneratedNamespace.YourDatabaseName.DefaultDataProvider =
SubSonic.DataProviders.ProviderFactory.GetProvider(
"your connection string here",
SubSonic.DataProviders.DbClientTypeName.SqlLite);
And you 're good to go.
For SubSonic 2, this answer sounds like what you want.

With subsonic 2 I use an approach where I inject the provider at runtime rather than loading it from the app.config file.
Look at my answer here: Subsonic in a VS2008 Add-In woes
Instead of just using one provider you could create one for every user who starts the application and change the default provider as needed.

Related

Choosing which database to use when starting the application

I'm developing a web application which will be use by 3 departments, and each of them will have its own database, because if a problem occurs (like a bad manipulation by a humain... which occured last week...) and I have to restore the database for one department, I don't want to impact the 2 others.
What I want to do is when my application start for Dept-A I want the application to use Database-A, and so on for Dept-B and Dept-C.
I know it is possible in the Page_Init code to set the datasource to the right database to use in the code for the current page, but in my ASPX pages all my SqlDatasource has a ConnectionString pointing to the web.config, that's what is blocking me. It would be easy if upon start I can set dynamically the database name to use in the webconfig connection string, but sadly it seems not possible.
For now my solution will be to deploy my app to 3 different website, each one will have the same code but their webconfig will contains the right database for them.
This will work but I just want to be sure that there is no easier solution.
Thanks!
Use a multi-tenant app. That way, you can set a different connection string per tenant.
It requires little modifications, for a simple app. You could also use a framework like ABP.

How do i update an existing database to support ASP.NET Identity 2.0

I have a web solution that I upgraded to ASP.NET Identity 2.0. The problem i now face it that i have some solutions of this software running in production at several customers. When I try installing the new software (with ASP.NET Identity 2.0) on the existing database (full of their data). I then get an error: "The model backing the "ApplicationDbContext" context has changed singe the database was created"
How do i update the database in the existing solution to a database that fits the new ASP.NET Identity? I looked at the ASPnetUsers table and there has been added several new columns.
Is there a way to disable this check? Or is there a way to fix it?
Personally, I would create a new project/database containing just the ASP.Net Identity 2.0 features and create a dummy user (just to make sure that it all works correctly).
Then, use schema comparison between the new database and your existing database. You can then generate scripts to create the user tables in your customers' database.
You may still have further integration work to do, but it should get you over the hump.
I suppose you don't use EF code first. You could run your project in dev using code first and get the full DB structure including the Identity tables, then compare against the prod schema and implement the differences.
This should give you the hint:
http://blogs.msdn.com/b/webdev/archive/2013/12/20/updating-asp-net-applications-from-asp-net-identity-1-0-to-2-0-0-alpha1.aspx

LINQ to SQL Connection Strings with class library projects

LINQ to SQL Connection Strings with class library projects
By default, creating a new LINQ to SQL model (.dbml) will put the
connection strings in both the application settings file and also
web.config / app.config. This is not so much of a problem for web
projects, but what about class library projects? i have a connection
class where I can use it to check connection in all pages but I have an
error where it cant read DataContext at all.
This is a photo that shows my problem.
Generally speaking, class libraries don't support config files. There are ways to make it work, but it's not considered a good practice since different applications may use the same library to interact with different instances of the database. I would recommend looking at a dependency injection or inversion of control solution like Ninject to pass the connection string to the constructor from the app that references the library.
UPDATE:
If you absolutely must read a config file from an assembly instead of the calling application, it can be done with ConfigurationManager.OpenExeConfiguration(). There are several answers here on SO that provide code samples for doing so, but I'm not going to link to them because I strongly encourage you not to go down that road.
By the looks of it you're not using LINQ to SQL - all I can see is an EntityFramework edmx. Check your code generation strategy, and make sure you're trying to instantiate the correct context name (think it's whatever the Entity Container Name is set to).
Also you need to make sure System.Configuration is referenced.
You need to put the connection strings in the main application app.config
Just put a copy of the connection strings in there and you can access them or in this case web.config

Store localization texts in database or resource files?

Currently in my Asp.Net website I store localization texts in a table in the database. The table has three columns:
TextCode nvarchar(100),
CultureId int,
TextString nvarchar(MAX)
So every time when I need to get a localized text for a specific culture I just do a simple query.
The main advantage of this method is simple and also I can update the texts while my web app is running.
The obvious drawback is performance. I use resource files for localization in other desktop applications. The reason I chose database is I am under an impression that updating the resource file in asp.net will cause the web app to reload and thus I need to take the site offline when I update the resource file. Is assumption true? What's the "common" approach to store localized texts in Asp.Net?
Thanks
Common approach is to use Global and Local resource files. If you want to use database consider using asp.net cache with sql cache dependency. In application start, load all your resource data from sql server and store it in cache, and create a utility class to return data from cache based on resource key.

EF 4.1 Code First and Existing Database and .NET Membership

I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box.
I added .NET membership tables to the database with no problem. When I tried to get Code First working I received the following error message:
The server encountered an error
processing the request. The exception
message is "Model compatibility cannot
be checked because the database does
not contain model metadata. Ensure
that IncludeMetadataConvention has
been added to the DbModelBuilder
conventions.
After some googling, I discovered that I had to delete the database and let EF create the database. That's fine but I lost all my .NET membership tables. I can go back in and add the membership tables again but if my model changes and EF needs to recreate the database then I have to add the membership tables in again.
How do I get around this?
This is how code-first work. Main idea of code first is that you do not touch your database because it is responsibility of the model to create the database. If you want to customize your database you must create custom IDatabaseInitializer and add your custom SQL.
public class MyDbInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
protected override void Seed(MyContext context)
{
// Here run your custom SQL commands
context.Database.ExecuteSqlCommand("CREATE TABLE ....");
}
}
Now you only need setup your cutom intializer on the startup of your application:
Database.SetInitializer<MyContext>(new MyDbInitializer());
If you don't want to do it this way you must manually maintain your database and set initializer to null.
Found a easier workaround here. I hope this helps.
http://www.paragm.com/ef-v4-1-code-first-and-asp-net-membership-service/
Another option could be to use the System.Web.Management namespace. I've had great success with the code below:
string connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseContext"].ConnectionString;
string database = "MyDatabaseName";
SqlServices.Install(database, SqlFeatures.All, connectionString);
It will just create the database and after that you can add users with the standard membership API.
Here's another possibility.
If you look at the MvcMusicStore sample - there's a SampleData class that is responsible for seeding the database on a rebuild. The SampleData class inherits from DropCreateDatabaseIfModelChanges, and overrides the Seed method. This class is passed to the Database.SetInitializer in the Application_Start method in global.asax.
I was getting the same error as you until I changed the parent class of SampleData to CreateDatabaseIfNotExist.
Then you can override the Seed method to insert any data you desire at startup, without it blowing away the database.
While you are developing, create 2 databases and two connection strings. One for SqlMembership (using aspnet_regsql) and one for your EF Application. If you would like to merge them into a single DB in production, just change the connection string in web.config.release to be the same. Then, EF model changes will just drop your apps db and not your membership DB.
By treating your authentication component separately, you will naturally decouple your authentication system from your application system. Then, if you wish to change membership providers, you will be better setup.
As the system grows, you will likely need to support non-pure models without EF code first, so this is a good template for going down that path.
I found the easiest way without playing with anything else was the following.
I ran the application first time with DropAndRecreatedatabase always in the Initilizer.
This created my database for the first time.
Following this I changed this to DropCreateDatabaseIfModelChanges.

Resources