Is it possible to see the data model diagram in vstudio when I use EF Code First? - visual-studio

Once I create my data model using Code First, I would like to see it diagrammed, is it possible? For example, when I use Database First, I generate an edmx. Is there something similar for Code First? Thank you

How about creating a new project for the same database , that uses model first? Just use the project to display the model, not update it.
If you are using SQL Server you can use the Database Diagrams from within Management Studio. Unfortunately I can't see the diagrams in the SQL Server Object Explorer in Visual Studio.

Related

Do VS Database projects and Entity Framework work together?

I've always been intrigued by Visual Studio Database Projects, and while they seem to be quite capable, I've never used them to any great degree outside of simplistic proof-of-concept work. I want to try this for a new project, and I'm also interested in using an EF layer on top of it, but in past test projects this has involved some decent effort.
I'm curious: has Visual Studio matured its product integration to support a single workflow that builds the database project, builds the EF layer on top of it, and finally builds the code, without intermediate steps involved?
We are a small team and we don't have dedicated SQL developers, and our primary goal is to bring the database into Visual Studio and to get it nicely under source control (TFS), and to achieve strong integration between from end to end. We're interested in growing into EF, and will probably start simple by treating it like a simple ORM tool to begin with if possible.
Has anyone actually done this that can provide insight into the process?
We have used VS2014, tool seem much the same and early version
Don't think has been much changes over the years
We have EDMX model and a DB project in the solution
Does mean that you need to keep the db project up to date.
But this is easy to do, you just publish you EDMX to local box/target
Then can import the changes with a schema compare of local to the project.
So they you can still have Model driven DB design
And use the DB project to deploy changes to the Dev/Stage/Live boxes
And can publish with automated deployments also.
The db project has a post build scripts option
Where you can use it to do seed data
And also a pre-build where you can do db manipulation if need to change structure and types of fields types when the data is on a live db.
Schema compare tool are rather good in Visual Studio
Can compare a DB to DB, DB to Project, or Schema file to either also

MVC3 Code First, Cannot find Metadata

I'm following a tutorial (http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx) to build a simple MVC 3 web application. I'm using a code first methodology and things were going good until I had to go back and add a field onto one of my models.
I got the error "Invalid column name 'Summary'." Which makes sense because this was the new field that I had added to the model.
Its my understanding that whenever Visual Studio realizes that the existing DB is different from the one that it is connected to, Visual Studio will try to recreate the DB. This is the behavior that I want so I added the following line to my Application_start in my global.asax file.
Database.SetInitializer<MyDBContext>(new DropCreateDatabaseIfModelChanges<MyDBContext>());
Now when I try to run the program I get the error "Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations."
I even deleted the existing database that I've been using so Visual Studio doesn't get confused.
What do I need to do in order to get Visual Studio to nuke the DB every time I change the models?

Save/Automate LINQ to SQL Adjustments in Editor

I'm generating domain model using LINQ to SQL via the VS2008 built-in editor. That works really well, too; when I adjust my database schema I simply delete everything from the editor and then pull it back in from the server explorer by selecting all tables and dragging them into the designer surface. That works great too.
Now the problem: I have properties that I manually set to autogenerated, readonly etc. using the property inspector on the right. Everything I re-create the entire schema I have to do this manually all over again.
Is there a way to persist these settings externally and/or automate them to bring it back to the state from before?
You can use something like the Huagati DBML Tools. This will allow you to update the DBML file from the VS designer.
I've also used the following process before:
Create my schema in SSMS
Create a script that uses the SQL Metal command line tool to generate the DBML file
As the DBML file is XML, you can run transformations on the file. I used this to simply change a few things like setting certain fields to be auto-generated (DateCreated, etc).
Then, either use SQL Metal or T4 to create the model files from the altered DBML file.
This process worked great - however I had complete control over the database schema. This process also allowed me to use L2S with SQL Server Compact Edition.
Hope this helps!
T4 Toolbox has a Linq to Sql Schema generator which allows you to develop your Linq to Sql applications in a model first approach. I have used it a little and it works really well, here is a blog post with details and usage info.
Your solution may appear to work when you have very few database entities / tables, but it does not scale and as you've found, syncing is less than ideal.
Do not use the Visual Studio 2008 LinqToSql O/R Designer
After looking at many alternatives to the problems you are describing with LinqToSql, I decided to abandon LinqToSql altogether as I didn't find any of the workarounds very good. Competing ORMs don't have the silly problems that LinqToSql has and they are much more mature and feature rich.
I could/should probably list some of the alternatives I ran across, but I don't want to spend the time and give you false hope, sorry.

Is there a better way to refresh Entity Models (*.edmx files)

I need to refresh a bunch of EDMX files in my solution. We have disected our tables into groups and each model represents one component or process. However, there are some overlapping tables, which means sometimes I need to refresh/update multiple Entity Models.
Refreshing a group of different entity models in VS 2008 is slow and dangerous. If I miss an out-of-date model, my application won't work
I need to verify that all of the models in my solution are up to date with my development database.
Ultimate solution: How can I script this? Is there an API to Visual Studio for refreshing an EDMX file? I do the exact same thing every time. Can't I program this?
Ok Solution: Can I set something up in Visual Studio to inform me when an Entity Model doesn't match a DB? What is the recommended way to test the model against a DB?
Thanks in advance.
Check out the EDM Generator
http://msdn.microsoft.com/en-us/library/bb387165.aspx
You could put in a pre-build event to regenerate the model.
It's also wise to pre-generate the Views which increases performance substantially.
Hope this helps.
As far as I can tell EdmGen will only work for WPF apps. Since you are looking to update a .EDMX file you will not have access to the xml files which the EdmGen tries to validate.
I would suggest checking the The ADO.NET blog: http://blogs.msdn.com/adonet/archive/2008/06/26/edm-tools-options-part-3-of-4.aspx
They have an explination of how to update an EDMX file but it is pretty involved. Hopefully VS10 will have a better solution for this.
Open the .edmx file in the Model Browser (double click it in VS.Net). Right-click anywhere and choose "Update Model from Database...". The wizard that opens up will show you a limited diff: new tables and deleted tables. But its granularity stops there. It doesn't show changes in fields, for instance.

Is it possible to update a sql database schema from an edmx in Visual Studio 2008?

So with LINQ-to-SQL I know you can update the database from the LINQ objects and update the LINQ objects from the SQL.
I am using LINQ's entity framework (a.k.a. LINQ-to-entities) and I can update the entities from the SQL database, but I can't update the database schema by changing the entities.
This is frustrating. Is there something I am missing? Am I doing something wrong? If this is not possible with Visual Studio 2008 is there a plug-in or other 3rd party application that will do this?
Right now the EF model can only be generated from the database. The database cannot be generated from the model.
Something that is talked about for future versions.
I have been working on extending the LINQ to SQL Database Sync(*) (csdb.exe) tool to work with edmx files. For now, I have got a preliminary, but working, version that can generate and update tables in your database. Relations are not working yet, and you will probably run into some other issues. The code is available on github, under the Ms-PL (as was the original csdb.exe).
Please note the database connection string and edmx filename are hardcoded for now. This is my prototyping version, not a release one ;)
(*) Please google the original tool. Stack Overflow does not allow me to post more than one link...

Resources