asp.net mvc Load Database from .mdf file - c#

I'm building an Web App using MVC, using Entity Framework.
Usually, the entity framework takes data from the database from the MSSQL server installed on machine.
I want to make at login a check, for example, if the connection at database is ok, if not, to take a .mdf file (stored on a cloud server) and use the data from that db.
This will be the 'emergency' database, backup etc.
Is this possible?

You can use DbContext constructor that takes connection string to the database. That's how you can handle to which DB you will connect.

Related

Can I create a database file without install a Database in c# Entity Framework first code?

hi I want to use Entity Framework for my Application. With my Application I create a Helpdesk Ticket and I want do this without install a Database on the server. for this I want to create only a file with the database data and content.
For SQL Server Express can I use mdf files. Is it possible to create a file how mdf or something.
The models are finish and I have a Context.
I ask this because a workmate ask me. He find it better if the application (asp.net mvc) start and on the server would be create a database file that save in a seperade folder, that I can select by the installation.
Have I a Change to do this?

How to protect ado.net entity generated database on a local machine

I have a C# application in .net 3.5 using SQL Server 2008 R2.
I am using ado.net Entity Framework to generate the database but after generating it when I remove the database file from SQL Server the framework does not generate the database.
I want to generate database on users local machine while installation and have a password on database access and protect the data from being read or modified by a local user rather than my application.
So the question is how to achieve this with out encrypting the whole data?
What is the parallels here?
I don't think EF in .NET 3.5 was ever capable of creating a database at runtime.
You'll have to either upgrade to .NET 4 and EF 5 or 6 to use code-first with migrations to handle this, or then you need to write some code to handle that situation yourself, in .NET 3.5.
Also: a SQL Server database (file) doesn't have a password mechanism like Access or other file-based system do. Access to a SQL Server database is handled by the SQL Server itself by means of logins on the server, users on the database-level and permissions for those users.

Oracle as file Database

Is there a possibility to create and access Oracle Database files from anywhere in the file system? I want to connect with C# and Entity Framework or NHibernate.
Background is we need to create a folder in the filesystem which represent a project and contains
the database itself
a very complex file and folder structure
the user wants to copy the folder to a new PC, and work with this project
I know SQL Server compact supports such scenario but we need Oracle as DBMS.
No. Oracle has no feature like that. Database files are created on the database itself, and they aren't as portable as SQL Server database files.

Using SQL Server databases on shared hosting

I'm attempting to deploy an ASP.NET MVC 4.0 application, and I am using a shared web hosting solution. I am able to successfully deploy everything, except for my preexisting databases, one for user accounts, and one for posts.
These two databases are in the App_Data directory of my application.
Every part of the application requiring database communication returns the following:
Invalid value for key 'attachdbfilename'
I should note that this problem does NOT occur when testing locally.
My web hosting provider has created a SQL Server database, providing me with a server IP, a username/database name, and a password. However I'm unsure how I can access it, and then replicate the current database in my application in the database on my web host.
Basically my question is, how do I get my SQL Server databases to work on my web hosting?
In this scenario you should not attach your own DB files. Create a new database (e.g. using SSMS or host's Control Panel) on the server provided by the host. Import your local data into that DB and use new server/db/uid/password in your connection string.
If your host already provided you the DB - just use existing DB (don't create a new one) but still import your local data into that DB.

Is there a performance hit for using MDF SQL Server files instead of "database"?

Currently my website is written in ASP.NET Webforms using a SQL Server database. I am planning to build a ASP.NET MVC application not because it's better but because I want to learn the technology. My question is more specific to the database. I can create the database and import my SQL table via the import feature of the web interface to the SQL database or I can use a "local" database MDF file. I like the idea of using a MDF file because it will be easier to backup and deal with. My website doesn't get a lot of traffic... matter of fact I could be the only user. :) Now here is the question.
How much of a performance hit will I get by using a local SQL Server MDF file instead for my database?
Effectively none. Or really none more than having the DB run on the same box as the web site. It is still a Sql Server Express database all the same. Production-wise, you probably want the DB running on a separate box dedicated to serving databases. But code-wise, the only difference will be your connection string.
SQL Server uses a MDF file for each database on its server. There is no difference between a MDF file and a 'database' because the 'database' gets stored in a MDF file anyway.
Performance wise you should not see a difference.
The biggest issue is with production deployment and management. It is MUCH easier to manage a standard database, than a dynamically attached .mdf.
Also, don't forget that your web host has to support this as well. And since SQL Server Express is the only SKU that supports "user instance" databases, the host will have to have Express installed for you to use it as-is. OTOH, you can develop with it this way and then just deploy your database and change the connection string when you deploy to the web host.
The only difference beyond the normal resource limits of the Express version of SQL Server is a negligable startup cost while the SQL Express engine connects to the MDF file, does its routine checks for file integrity and transaction log stuff.
This should only happen on application start up, not for every request.

Categories

Resources