Entity Framework: existing database -> data model -> new database - c#

I have created a new MVC3 project and used the ADO.NET Entity Data Model to map out my existing database. It worked great.
Now, that is a database I don't want to affect when doing development of my web app, so I was looking to now attach that data model to a local database (haven't selected one yet, suggestions welcome. I was thinking SQL Server CE or MySQL).
Does anyone have any ideas on how to do this? I'd be nice to keep the existing connection string to my live database as well as this development database environment.

You can generate another matching DB using whatever technology you want (though it would be good to keep them similar to your production DB) and then have a Web.Debug and a Web.Release setup.
If you look at swapping out the configuration, connection string portion of your web.config file you can easily swap from development to production.
http://msdn.microsoft.com/en-us/library/dd465326.aspx
This provides the relevant information for the transform statements in your Web.config file, but from the sounds of it you can simply swap the connection portion of your web.config and get what you want.

Is the existing database on your machine, or on a server somewhere?
If it's on your machine, just back it up and restore it to a different database; i.e. if your database is called XYZ, back it up and restore it to a database called XYZ-Testing. You can then connect to and use XYZ-testing without affecting XYZ.
If the existing database is on a server somewhere, the easiest solution is to install SQL Express on your machine, and then backup the database on the server and restore it on your machine.
Doing this will make your life much easier than trying to use a different database type (SQL CE or MySQL).

Another option would be to use SQL Server Data Tools (SSDT) free download, add-in to VS or stand-alone if no VS, and then to create a local database project for development. I wrote a blog post about using SSDT as well.

Related

What is the most efficient way to deploy a WPF application together with a SQL Server database?

I am building a WPF application that connects to a SQL Server database. I am wondering how I should go about, deploying the application on the client's computers together with the local database.
My initial thoughts are (assuming that the client computers have SQL Server installed):
Deliver the .exe file of the application
Provide a script file that runs my stored procedures for creating the database and tables
Is there a better way of doing this? I could create a service inside the application that sets some sort of boolean value in the registry of the computer and runs the "init-database-scripts" only the first time but it feels wrong.
Any other suggestions?
Thank you!
First off, I would strongly consider making an actual installer using a tool like WiX. It can run the script for you at install time so you don't have to deal with the registry and such.
There is another option assuming you are using Entity Framework - Code First; you can use a database initializer to create your database. If you never think your model will change DropCreateDatabaseIfModelChanges will work, otherwise enable migrations and use a migration initializer.
To use the initializer you picked, make sure to call Database.SetInitializer in your applications startup routine.
Add a SQL Server database project to your Visual Studio solution. When you build the project, a Dacpac file is created which can be installed on a client machine using SQLPackage.exe. Basically, your app installation script/software calls SQLPackage along with your Dacpac file to install/update the SQL database. Here is a link to a tutorial on setting up a SQL database project: Tutorial

detach an SQL server from visual studio

I'm pretty new to developing in Visual studio and working on databases.
I am working on a program that deals with reading and writing data to a database that I created with Visual studio.
I need to work on this project from another computer and copying over the project files was a breeze but I'm facing issues when it came to copying the .mdf database file.
Upon research it seems like, at least in the Microsoft SQL Server program, that I would have to "Detach" the database before copying it over to a different computer. So I am assuming I would have to do something similar with my Visual Studio Database as well.
Anyone has any inputs with regards to this?
If there is not much that I can do, I guess I could recreate all my tables and everything in Microsoft SQL Server program, so that it would be easier to move the database if needed.
I was in a similar situation such as yourself when I began developing my first core application. You have a few different options including:
Detach an already created database from the hosted SQL Server service and "re-attach" to another SQL Server service that is accessible from the desired set of hosts. You have to essentially disconnect the database from the service before you are able to transfer or migrate it since the process will have an exclusive lock on the .mdf file. https://msdn.microsoft.com/en-us/library/ms190794.aspx
Create the necessary .sql scripts to construct the database and run in the appropriate order e.g. create database, create tables, etc. to re-construct the database at the service location.The neat thing about this technique is if you have already created the database (which it sounds like you have) SQL Server allows you to generate scripts rather than having to write them yourself. https://technet.microsoft.com/en-us/library/ms178078(v=sql.105).aspx
Finally you may use a subscription based service such as SQL Server through Azure to host the service for controlled global access aka DBaaS (Database as a Service). I can't post anymore links, but look at Microsoft's Azure SQL Server hosting service if you are curious about this option.
The unfortunate part you have to decide is how much time you would like to invest in this. I began developing the application from scratch which led me to developing scripts to conjure up the database for deployment purposes. Good luck!

Creating and saving a database in my project?

I'm working on a Windows Forms application in VS 2013 that requires saving data. I can't find a good solution for this, since:
A SQL Server database will work fine, but it will force the user
to have SQL Server installed on their computer.
The same applies to Access or Oracle DB.
DataSets require a database connection,which leads me to 1) and 3).
Text/XML files don't satisfy the
security requirements and will seem like a very primitive solution.
So , in essence, when the user installs the application, on its
first deployment it must create a database and keep it for future
access, without requiring the user to have any special programs
installed (e.g. SQL Server). I apologize if this question seems
stupid.
This is in two parts - how to create a database application and how to access it for initialisation.
Option 1 - if you can resolve the problem of installing SQL Lite/compact using the suggestions above then you can use Entity Framework with code first to create the database and tables. There are plenty of examples only a Google search away.
Option 2 - create an Access database (an MDB or ACCDB file) with blank/empty tables and deploy this as part of your application. You can the access this with a suitable connection string - again, Google will solve that one.
Use SQL Compact Edition .
Using LINQ to SQL you can create a Database/Tables for the first time deployment.
Please Refer the Link http://msdn.microsoft.com/en-us/library/bb399420(v=vs.110).aspx

Easiest way to set up SQL Database for local testing of C# project in Visual Studio Express 2013

Okay, so this is going to sound stupid or trivial, but please, humor me. I am writing a personal project in C# using Visual Studio 2013 Express and some of the code is going to hit a SQL database. Before I hit other databases, I want to test it locally and make sure that it performs as expected. However, I can't seem to find any tutorial that either is complete or works in order to get a SQL database up from nothing.
First, I tried installing SqlLocalDb 2014. Even after the tools I can't figure out where there is a UI that I can type in scripts to create a database.
I tried the directions here: http://www.wikihow.com/Create-a-SQL-Server-Database
But at step 3, the option to create a database is greyed out, and searching that issue got me nowhere.
I've tried adding a new SQL Server Database Project to my solution. This looked like it would work exactly, but when I designed the table I couldn't figure out how to actually deploy/publish it so I could connect to it. I get a SQL file, and I can't figure out what to do with it from there. I found instructions on how to publish it, but it seemed the only directions were to publish to a remote server (and I want to use a local one).
I tried the instructions here (yes, I know they're for 2012): http://msdn.microsoft.com/library/ms233763(VS.110).aspx but I can't find the Server Explorer / Database Explorer. I found the Server Explorer (where my table doesn't appear) and the SQl Server Object Explorer, where my table does appear but I can't figure out how to populate it with dummy data or how to connect to it.
This should not be this hard and I feel like I'm missing something obvious. Thank you in advance for any help.
Hitting F5 deploys your SQL Server Database to a localdb instance that you can connect your application to. You can see the connection string for this database by right-clicking on the project -> Properties -> Debug -> Target Connection String.
You can use this connection string in your application code to connect to the local database and test it - place it in a configuration file that can be replaced with the real database connection string in your production code. Alternatively use a database first Entity Framework Model to connect your application to your database, and you can just change the App.Config file to specify whether to target your test or production database. That's a really nice way to handle this since it will produce all the code you need to work with the database.
You could use xampp. From there you can create local databases and then you could talk to the created database using "localhost" instead of the productive server.
For the record there is some information on Accessing Data in Visual Studio at:
https://msdn.microsoft.com/en-us/library/wzabh8c4.aspx
And specifically to answer your question there is a "Walkthrough: Creating a Local Database File in Visual Studio" at:
https://msdn.microsoft.com/en-us/library/ms233763.aspx

Visual Studio 2012 move local .sdf file to server computer

here is the problem I am facing now. I have created an application that uses local database (this was created by Add -> New Item -> Local Database. Afterwards I have added tables under this .sdf database.
Then I have connected to this database using Add -> New Item -> ADO.NET Entity Data Model.
Everything works like a charm, unless I was asked to move this database to a place, where multiple people could access this database and work with it.
Therefore, as I have no previous experience with databases, I have treated this .sdf file as any other file (let's say Excel workbook) and I thought that I could simply take already existing database, copy it on server computer (e.g. \Server001\Database\Database1.sdf) and simply change connection string under app.config.
However the problem is that this does not work. As I didn't know how to change connection string, I created new application, where I have tried to connect to this database located on a server computer; however I received the following error:
SQL Server Compact does not support opening database files on a network share.
I already have fully functioning program, but I have no idea how to make it work with multiple users. I have tied to google for solution, but all I could find is how to create local database, not how to make it accessible by placing it in server computer.
Could you guys please help me? If you need more details, please let me know!
P.S. This is WPF application, .NET 4.5, created using Visual Studio 2012 Professional.
Thank you!
The error message pretty much sums up the problem: SQL Server Compact does not support opening database files on a network share.
SQL Server Compact (aka "local database") is to be consumed by a local application; even if it was a web app serving many requests, the application itself is local.
If you want to have multiple remote connections (i.e. centralized DB, distributed app), you should look at using an instance of SQL Server (any SKU would probably work, even SQL Server Express). Those will use MDF files instead of SDF files, so you might want to refer to Convert .sdf database to .mdf database. You'll probably also need to set up a user identity for your connection string, so check out this link on CREATE USER and Difference between a User and a Login in SQL Server to understand how that can be configured.

Categories

Resources