EDMX files created - entity-framework-4

When I created a new .edmx I got the following files
under
->Model.edmx
-->Model.Context.tt
-->Model.Designercs
-->Model.edmx.diagram
-->Model.tt
A previous copy of the edmx file that was created before
just has the following:
->Model1.edmx
--> Model1.Designer.cs
Just wondering why the one I created has so many things underneath
it while a previous copy has less.

By default VS2012 used SingleFileGenerator that created ObjectContext based context and EntityObject based entities. In VS2012 it was changed - now the code is generated with T4 templates (the .tt files) which generate your entities and context. The default entities generated in VS2012 are POCO entities and the context is DbContext based. If you want the ObjectContext back just delete the .tt files open your model in VS, right click on the designer surface and in properties change Code Generation from "None" (meaning T4 templates) to "Default" (meaning ObjectContext). The "Default" being not really the default in VS2013 was a bit confusing to people. In VS2013 we changed the names to "LegacyObjectContext" and "T4".

Related

Some entity framework model files are not being added to the project folder 'Model'

I have an ASP.NET MVC application which is using Entity Framework.
One of the projects in the visual studio solution uses Entity Framework and contains an edmx file under Model folder. Within this folder there are some model files that are automatically generated as a result of the entity model.
I have noticed that in this model folder are missing some model files, I mean, there are not included in the model folder. In the model folder there are some model files but not all of them. The missing model files exist in the physical path so I try to add them to the project under model folder but afert thgat they continue not appearing there.
How can I successfully add the missing model files to the project folder "Model"?
I am using Visual Studio 2013 and .NET 4.5.

If I edit an EDMX file, does it make changes to any files besides itself and it's designer file?

I made a change and undid it to my edmx file, but it hasn't worked the same since. Wondering if another file was changed behind the scenes that needs refreshing?
The .edmx file contains the model mapping metadata that is copied into your assembly as Resources at build time, and several other project files are generated at design-time based on the .edmx file. The DbContext type and all of the Entity type definitions are generated as source code files from the .edmx file, and Visual Studio re-generates them after any changes to the .edmx.
If you alter the .edmx file outside of visual studio, or perhaps revert just the .edmx file from your source repo, your other generated files may be out-of-sync.
The design-time generation is driven by Visual Studio's Custom Tool functionality. So you should be able to right-click on the .edmx and select "run custom tool" from the context menu, and then do the same for any .tt files nested under the .edmx in the Solution Explorer to regenerate all the source code files.
And, as a last resort, if you don't have a lot of customization in the .edmx you can simply delete it and regenerate it from the database.

ADO.NET Entity Data Model Wizard - Model Class file not getting created

Used the "ADO.NET Entity Data Model" Wizard.
Gave the corresponding connectivity and once the wizard completes, the .edmx file opens with the diagram containing my table structure.
But the Model Class file is not created.
How to overcome this error?
I even build the solution, also checked by using "Show All Files".Nothing works out.(using VS2010 SP1, EF6)
Deleted the project and tried again and then was able to find the model file.
This is not the correct answer but as it solved the issue, this might be an answer for this issue.

How do I create my own Scaffold Template in ASP.NET MVC 3?

ASP.NET MVC provides the ability to select a 'Scaffold template' upon which a newly-created view will be based (Add View > Create a strongly-typed view > Scaffold template).
Is it possible to create your own Scaffold Template? And if so, how?
ASP.NET MVC uses T4 templates. Here's an overview.
Here are the steps:
In the Package Manager Console type: install-package mvc3codetemplatescsharp
Accept all the warnings
The CodeTemplates folder will be added to your project containing the templates
From here you could either modify the existing templates or add new one.
Or if you want to modify those globally you could to this in the C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\ folder.
You can use T4 without nuget of course: Place a folder in the root of the application website (the project containing the views). The directory structure is important so it should be
\CodeTemplates\AddView\AspxCsharp\MyTemplate.tt
You can copy the contents from one of the existing templates located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\
Next, clear the property on the TT file named "Custom Tool". This should be blank.
Then right-click on any Controller Action and say "Add View" or since the controllers are in a separate project in our case, right click on the View folder and click "Add View".
From the dropdown Click "Create a strongly typed View" and then enter the type to use under "View Data Class:"
Finally, in the "View Content" dropdown, select "MyTempate". This should show up if you've entered the folders correctly.
In Visual Studio 2012 with MVC 4, the easy way (install Nuget package) gets you an incomplete setup, because the Nuget package is woefully out of date (last updated in 2011 - perhaps the day it was created).
You have to use the equivalent of Francis Shanahan's answer, but instead the path to copy things from is (64-bit):
C:\Program Files (x86)\Microsoft Visual Studio\11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates
You:
Add a Reference to Microsoft.Web.Infrastructure, which you can find in Assemblies > Extensions.
Copy this CodeTemplates folder (including the folder itself) to the root of your Project and Include it in the Project
Ignore the compiler errors for now (like can't find MvcTextTemplateHost)
Go through the Properties of each of the added files and delete the text in the "Custom Tool" property of each. When you do the expand arrow next to each file will disappear (because the file will no longer be generated in-place)
Save All and build - compiler errors gone
If the compiler errors don't go away - especially if you're seeing an error in a generated .cs file Visual Studio can't find, and a .tt file - close Visual Studio, wipe your temp folder, and reopen the solution. In one case I went so far as restarting before the issue cleared up. It was caused by a generated .cs file from a .tt template that Visual Studio was still trying to automatically generate code for.
In addition, the names of the .tt files are a little bit confusing - here's how they map:
GUI:
Empty MVC controller
MVC controller with read/write actions and views, using Entity Framework
MVC controller with empty read/write actions
Empty API controller
API controller with read/write actions, using Entity Framework
API controller with empty read/write actions
CodeTemplates\AddController's files map respectively:
Controller.tt
ControllerWithContext.tt
Controller.tt
ApiController.tt
ApiControllerWithContext.tt
ApiController.tt
There's an if statement in Controller.tt and ApiController.tt that handles the with/without read/write actions functionality.
For the views, naming is intuitive except that List.tt creates Index.cshtml, and Empty.tt is used for any View besides Create/Delete/Details/Edit/Index.
This question covers what properties you can use in the .tt templates.
.tt templates are Microsoft T4 templates. T4 Template Syntax.
Or, for Visual Web Developer Express on a 32-bit system, another location for these files is C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\VWDExpress\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates
This MSDN article discusses Scaffolding and Page Templates:
http://msdn.microsoft.com/en-us/library/cc488540.aspx
Which, in turn, links to the following article for modifying the templates:
http://msdn.microsoft.com/en-us/library/cc488537.aspx

Entity Frameworks 4 - Changing the model does not update the T4 self tracking template files

I am using self tracking entities and have moved the entity classes to another assembly by using 'Add as link' to point to the TT file as mentioned here. Now though, when I update the model (for instance change a property name) the template is not automatically run and so the entity class does not get updated.
I can of course manually run the template to get the updates, but it would be easier if it ran automatically in the way it did before I moved the classes. Is there any way to achieve this?
Darren.
I've not done this but I suspect the following is possible. Mark the model with a custom tool (see under properties of the file in solution explorer). Then create a small program (see here for an example) that will execute a the TT's run custom tool.
I have not run into this issue. Usually when i save the model or build the project which contains my edmx file, the code files for my entities are updated. can you share your project with me through direct email. I wouldn't mind trouble shooting the problem and then following up with team if we cant get it to work.

Resources