Creating entity diagrams from code first classes - entity-framework

I was just reading Asp.net MVC3 tutorials (Models (Data))
On this page tutorial 4 of 10 on the ASP.NET website, it is shown that an entity diagram is created from code first classes. How to generate them?

This can be done very easily by using the Class Diagram. Add New Item > Class Diagram. Then drag and drop your code first classes into the diagram from the solution explorer.

A Class Diagram is OK, but it doesn't automatically show the relationships between classes. The slickest way that I have used is Entity Framework Power Tools. Their description of the tool:
When right-clicking on a C# project, the following context menu function is supported: 1) Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database.

Assuming your schema has been created from the Code First classes you can reverse the db into a an edmx to visualise the Model. Any classes generated from this obviously won't be related to your Code First classes though.

Create a copy of your project. Open the copy and add a new item/ADO.NET Entity Data Model. Edit the diagram for layout and print to a .pdf file. Delete the copy of the project.
Anytime you make a change you will have to re-create the diagram and edit the layout, but I can usually get through the whole process in about 15 minutes.

Related

Does EF Core 3.1 support use of SQL Views inside of code-first DbContext?

I currently have an ASP.NET Core project using EF 3.1. I would like to use Code-First to manually build most of my entities, but there is also a linked server I need to incorporate data from as well. I need to figure out how to create an entity model that is the result of this view (that includes a left join from a code-first table I have already migrated). During my research thus far, it seems that since EF3 there is support for views inside of Scaffold-DbContext, but my concern is I would only want to scaffold this single view, but still access everything inside the same DbContext I am already using. I don't want to hack things together, so please let me know how you would accomplish such a task in your project. Thanks for your help!
Looks like I was able to figure this out after all. The steps I did are as follows, but please correct me if you know a better or more accepted way.
Selected the SQL view into a new table so I could generate a create script off of it.
Copied the create script into the class creation tool over at https://codverter.com/src/sqltoclass so didn't have to manually convert 20+ columns and types to class properties.
Copied the generated properties into a new model class named after the SQL view
Added a new DbQuery under my existing DbSets in my DbContext class. (It does seem like DBQuery is deprecated, but I don't see another way around using it currently) and named it the same as my SQL view.
Scaffolded a new controller off the SQL views model class and navigated to the generated index page to verify data could be seen. (it worked!)
Let me know if you have any better ideas on how I came to this solution!

Is there any way to create diagram for localdb?

I have created a database using Entity Framework code first approach, and now I am trying to verify the relations of data tables but unfortunately after wasting 2 days still unable to get any solution.
According to this article the following query solves the problem.
alter authorization on database::[your-db-name-no-quotes] to sa
I tried it and it worked for me.
You might try to use Entity Framework Power Tools that is a Visual Studio add-in that was used to create the data model diagrams shown in these tutorials. The tools can also do other function such as generate entity classes based on the tables in an existing database so that you can use the database with Code First. After you install the tools, some additional options appear in context menus. For example, when you right-click your context class in Solution Explorer, you get an option to generate a diagram. When you're using Code First you can't change the data model in the diagram, but you can move things around to make it easier to understand.
There is a possibility to create a diagram from an existing database. You could use SQL Server Management Studio Express diagram generator. Right-click in diagrams folder in the database and choose create a new diagram.
Please also refer to this answer: Generate table relationship diagram from existing schema (SQL Server)

Entity Framework: Added view appears in Model Browser but not Solution Explorer

I have a project which uses Entity Framework. I've added a view from my SQL Server database to the model using the Model Browser in Visual Studio 2012 such that it appears under the following items:
Entity Types under my model.
Entity Container, Entity Sets
.Store, Tables / Views
However, I can't access it from my code.
Back in Solution Explorer, under the Models folder, the view does not appear anywhere under the EDMX file for my model (though it DOES appear in the diagram).
I can not seem to find any practical way to add the view to my data model such that it is usable. Ultimately, I want to reference it via the entities object so I can select data from it.
Every Entity in EF should have unique identifier. Try to select a key column in view and then try to add view. View should be available to you

T4 template and EF with Code Generator from database schema Application

I am currently in process of putting a process (using a windows application) in place where EF's POCO objects classes can be generated by pointing this app to the database table(s).
I have seen T4 templates and the VS Addin which helps to achieve this in VS. However it requires T4 template file on the VS solution. We do not want to go through this route. What we want is to just copy the classes generated by this app to the solution so it can be used.
The way T4 template works is that as soon as you make a change in the template it applies to the Class file. What I would like to do is to point the new App to the database and say tables A,B,C. The app will read the columns,types,relationships and create corresponding classes to a folder. Developer then Copy these classes and paste into VS solution. Longer terms plan then to extend this app to write repository classes.
I have rough idea but not a clear picture. Does anyone has any pointers about how about can I go to achieve this?
Thanks
Edit: This is purely in mind using EF Code first.
What it seems you are asking is "Can my T4 templates live in a separate solution to my output code?".
Yes. You can do that.

WPF and entity Framework code first

I would like to implement a simple WPF with a datagrid and a save button.
when I click save button it will accept changes (row edit,cell edit, new row, delete etc)
I tried RowEditHandler and CollectionChange event using observable collections. But I couldnt get a soluton. Can anyone please show me a simple way.
Using dataset (xsd), I was able to achive that simpy by sending datacontext of grid to dataset and using update function.
thanks for help
The ADO.NET team blog has an example how to bind a Entity Framework Code-First model to a WPF DataGrid which supports adding, deleting and editing entities in the grid and finally saving all changes. It is focussed on a Master-Details scenario but should with a few modifications also work in your even simpler case with only a single DataGrid:
http://blogs.msdn.com/b/adonet/archive/2011/03/08/ef-feature-ctp5-code-first-model-with-master-detail-wpf-application.aspx
The example is based on EF CTP5 but it will most likely also work without changes with the new EF 4.1 RC version.
The ADO.NET team example uses code-behind files and events. If you prefer to strictly work with the MVVM approach the solution of David Veeneman provided in this answer might be helpful:
Entity Framework 4 and WPF
It's not a full WPF example but can serve as a foundation how to extend an ObservableCollection in a way that it supports Create-Update-Delete operations with Entity Framework.