Determine if SQLite database schema is current - c#

I'm using SQLite and Entity Framework code-first. And my Windows desktop application needs the ability to Create and Open files.
For creating a database, I found DbContext.Database.Migrate(), which will create the database if needed, and them make the schema current.
For opening a database, I'm not as certain. I may try to automatically updated it, but I might also prompt the user before doing so.
I found DbContext.Database.GetMigrations(), but how would I use that to determine if the database file is already current?

I also found DbContext.Database.GetAppliedMigrations() and DbContext.Database.GetPendingMigrations().
DbContext.Database.GetPendingMigrations() can be used to get all the migrations that are not applied to the database.

Related

SQLite: How do I set up the Data Source of a connection string to be a file within my application folder?

I am creating an application that needs a database as it's data store. I have installed SQLite in my project (including Entity Framework 6.0.0) as well as the extension SQLite Server Compact ToolBox.
I am trying to create a database in a local file using SQLite but I'm unsure of the format for doing so in the Data Source part of the connection string. This is using a code first approach.
I have looked at 20+ videos and read numerous tutorials online about how to set up databases using Entity Framework over the past couple of months and still am lost about how to set up and run a database from code. I can create the Models and the Context, perform CRUD operations on a database I have created somewhere in my program files (using a default connection string I am guessing), but am still none the wiser about where they are installed and connection strings, specifically Data Source, I want the database to be shipped with the application not requiring a separate install.
Every time I come back to adding a database to my Application I have got frustrated at the lack of clear tutorials on this subject, including one I paid for! So I leave it till later, but it is the last hurdle in my Application so now is 'later'.
Any help would be appreciated!
TLDR: I need to know what the syntax/format is for a Connection Strings Data Source targeting a folder/file within my project using SQLite.
To create connection string simply write
Data Source=NewApp.db
and that's all

How do I create a database that's already included in to the project - C#

Is that possible to create a database that's already included in the project and without opening the database application?
Because, I have to publish the system that I made.
I think that you might be interested by in-memory databases, It is treated here.
You could also have a remote database, giving the capacity to anyone with your application and appropriate credentials to connect to it.
If you are looking for a other alternative, you could also export the Scheme and/or Data of a database to a portable .sql file.

Entity Framework DB migration

In my Application I have used Entity Framework Database First approach.
Currently my application is in Dev Environment, now it need to be moved into Test Environment and later into Production Environment.
So is there anyway that I can use .net feature or Entity framework feature to migrate/create database in Test environment. Other than using SQL feature of restoring the database.
Also note that if any enhancement comes then Database structure can change, table schema can change.
So can you suggest me the best way to easily migrate database schema in different environment without losing existing Data.
If you want to take advantage of EF-Migrations feature, you must convert your application to Code First with Existing Database http://msdn.microsoft.com/en-us/data/jj200620.aspx
If you are unable to convert to code first then you must create the update script by hand.
Use a schema compare tool, compare the development and production server.
For each difference found, create an update query.
Once the entire script is finished, test it on the staging server.
Automating the migration is very risky, it depends on the type and size changes you made to the schema. You can't trust any single feature or tool specially if the changes requires data motion (moving data around).
The following links might help you:
How to do Migrations in DB first approach
EF Migrations for Database-first approach?
With Database First, the easiest way to copy a schema is to extract a data tier application in management studio, create an empty database on the target, register it as a data tier application with the same name, and upgrade the empty database using the upgraded file. You can repeat this step to manage schema changes.
Having said that, going forward you're really better off switching your Database First to Code First as it will make change management across your deployments much easier.
Migrations are best way to deal with it
Preferred way to update production db is to first generate sql file and then run the sql file in production environment.
MS had a very good artical on this
http://msdn.microsoft.com/en-in/data/jj591621.aspx#script

Where is my database in Visual Studio 2010?

I'm creating my first project in (C#) ASP.NET MVC3 using Visual Studio 2010 Professional, I'm creating a very basic blog system. During the tests I created a some tables and now I want do store different data on those tables but I keep getting this error:
The model backing the 'CategoryContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
Obviously I need to update the tables in database but I can't find where I can access the tables. I can open Server Explorer using CTRL+ALT+S but there is no Database or tables there.
Here is the screenshot of it:
http://img710.imageshack.us/img710/8944/screenshot001kd.png
Where/how can I find and/or edit the actual database tables?
Thanks.
Well here is the best I can do for you:
If using SQL Compact Edition then you should look into your project folder on the filesystem and look for a folder called app_data and you should have a database in there.
Alternativly you could just open your web.config file and look for the section that is the "ConnectionStrings" section and just look for the location of the database.
If it is in SQL Server Express, then in your Database Explorer window, click on the database connections and create a connection to your local sql server express. You should see it in the list of databases as the machinename\sqlexpress.
From your error I'm going to guess that you're using Entity Framework's code first approach.
What happened is that Entity Framework has created a database for you on the local sql express install. To find the actual database, look in your web.config file and find the connection string section which will tell you the server and the catalog for the database. You can either us Visual Studio to access the database by using the data connection area found in the server explorer panel in your picture. Or you can use Sql Server Management Studio to do the same thing.
Now the reason why you're getting an error is because Entity Framework created that database based on the entities which you initially defined. Once you have made changes, the database no longer matches the entities and you get the error listed above. Your solution is to either delete your database once you have found it and allow the auto-magic to happen again, or you will need to do some changes to your code to not auto-generate your database each time you make scheme changes.
I would suggest you look through MSDN's ADO.NET Entity Framework 4.1 and read up on 'code first' development styles.
Good luck, and hope this helps you some.

Linq-to-SQL: how to handle database changes

I have several installations of my Linq-to-Sql app running in the field. Now I've created a new version, which adds a new column to a certain table. I've added this column in the dbml file. But when updating the installation, I want to preserve the existing database. How to handle this? Linq-to-SQL doesn't seem to like this inconsistency.
Is there an easy way to update the existing database using my new dbml file?
You need to manage your database schema explicitly - that is to say that you should have creation and update of the database schema scripted so that its repeatable. For the scenario your describing I think that your application should (ideally) create and then update the database schema as required. The initial work to set this up isn't too hard and once you have the system in place making schema changes is straightforward
I wrote this up (in terms of what has worked for me for a lot of years now) at some length here:
How to create "embedded" SQL 2008 database file if it doesn't exist?
Which probably ought to be modified to take advantage of this which talks about using database extended properties:
SQL Server Database schema versioning and update

Categories

Resources