How To View Database Diagram? - c#

I am working on a project which requires me to write several POCO classes using Entity Framework Code First. There is a lot of entity relationships and inheritance going on and its hard to keep track of everything just looking at the code. Now, as we know, Entity Framework Code First yields an .mdf file as your database, and i was thinking for verification, a database diagram would server me better.
Is it possible for me to view my database diagram in this scenario, and how may i do so??

You could always point it to a SQL Server Express database - by default an MVC 4 project uses LocalDB but if you're more comfortable in management studio you can always create your own database and change the connection string to that.
Also from memory I believe you can also attach an mdf file in management studio but may have trouble while the application is running. But I could be thinking of something else there.

Related

Is database recovery from EF model possible?

I had a web service project, with a database integration of course :) I use Entity Framework, so I generated my EF model from my db (sql server express). Everything was okay till my computer got broke. I had my project files backed up so I recovered them. But I didn't have the db backup(shame).
As far as I know there is this concept Code First, and what I am wondering is can I use it to regenerate my db? Otherwise I'll have to try getting the db from the old disk, or rewriting the whole db.
Before going into those struggles I wanted ask and get a proper answer to this since I thought it could help others too.
Of course, you can.
Specify a connection string in your DbContext constructor, which points to a non-existent database, and the database will be created from scratch.
The EF workflows are "reversible" in the sense that you can go from DB to model or from model to DB, no matter if the model is a graphic EDM or a DbContext.

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

Entity Framework will not load ALL my associations when using a database-first approach?

I created a DB-model in Microsoft Access 2013 in which I have rules and associations made. I there-after transfered this DB to Microsoft SQL Server 2012 with full success, the associations are all still there and seem to be exactly the way I want them to be.
You'll have to excuse me for removing so many parts of it, however the general idea should be clear even with me "censoring" it; This is how the database diagram looks in SQL Server 2012:
http://i44.tinypic.com/ay51dy.png
However... I'm using Visual studio 2012 with Entity Framework Version 6.0, and with a database-first approach. The results are not what I expected, as my .edmx-file will NOT transfer all the associations which already exist and can be seen even from SQL Server. All the tables are transfered just fine, it's purely the associations which only 1/4 of them all are actually transfered and shown, manually adding them all eventually ends up with the last one not wanting to work no matter how much I try (Error 111).
To make it as simple as possible; Using the database-first approach, my person-table will only have one association autogenerated, but as you can see above it should have 10 more. This happens all around, certain associations are autocreated and others seems to just be dismissed for no apparent reason.
Does anyone have any idea as to what is causing this issue, how to approach it? I'd rather not start with modeling everything up again directly into Visual Studios designer and then make it create the DB with associations and such.

Moving Entity Framework projects

I have developed a project using entity framework on my local machine. What I am wondering is what is the best way to move the project to live server. Do I need to recreate the entities on the live server, if tables are same? Or I need to change the connection string? Reason why I am asking is that on live server I might not have access to the Visual Studio to re create entities.
I searched around cant find what I am looking for. Appreciate your comments.
You need to create the database and objects on the server manually. You could export the DB schema to a SQL script through Management studio.
Also ensure that you get rid of the DropCreateDatabaseIfModelChanges strategy, which might be the case on your development environment- especially if you are using rapid prototyping.
If your connection string is externalized to web.config, donot forget to change that as well.

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.

Categories

Resources