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.
Related
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!
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)
I've been developing simple PHP/MySQL web sites for some years.
Never used a PHP Framework before and I understand I'll need to know OOP, no problem.
I'm about to start a SaaS project of my own.
A)
So far, I've seen Yii generates the CRUD and pages according to the DB.
Is it easy to modify the generated code?, like, adding a new DB field and its form field without not generating again all the stuff every time I change something in the DB and losing other customizations?
I mean, I'm 100% sure the generated DB code and pages are not going to be enough and I'll be constantly adding and correcting fields, and adding more tables etc.
B )
My project will include a Shopping Cart and Calendar(for events, tasks, etc.).
Does Yii has these options or at least an easy way to implement it like the Authentication options or Database listing, etc.?
C) Does documentation has this explained as a tutorial/book or is more like a reference(minimum explanation that only advanced user understand how to integrate it)?
thank you very much
Yiiframework has excelent documentation (you can start from here). Also there is an extensions area in the downloads section where you can find all available yii extensions.
All your questions can be answered if you follow their easy tutorial.
A) Yes, it's easy. You will just add code for new fields not changing it all.
B and C are answered by Stratosgear very well.
Is it easy to modify the generated code?
Yes it is. If you later decide to add more fields to the table, you can do that from your Phpmyadmin using sql commands.
You also need to edit the generated class file adding those new fields to correspond with that on your table.
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.
After I've generated the interface/implementation files for entities of a model file in XCode, I've not found a way to keep any custom code (validation methods, etc...) I've added to those generated files, given the scenario where I've added an attribute to a model entity and need to re-generate the interface/implementation files. Does anyone know of a way to make this happen? I've just been doing the copy/paste shuffle, but there has to be a better way.
Assuming that you're only talking about adding methods, and not new instance variables, I'd recommend using Objective C categories to add additional behavior to your model classes. Here's a blog post along the same lines.
Use mogenerator, which uses the Generation Gap design pattern to prevent your customizations from being overwritten when the code is re-generated.