.NET Entity Framework 4.0 - prevent overwrite - visual-studio-2010

I have a ASP.NET 4.0 Web Application Project, it work with edmx model file generate by MSSQL 2008 and named StaffModel.edmx
I have my custom logic and apply to StaffModel.Designer.cs ,today i open the edmx ,but not re-generate form database, just have a look, but VS2010 overwrite my *.Designer.cs in accident!
No message or confirm to ask me overwrite or not, so how can i disable that in VS2010?

Generally you cannot. .Designer.cs files are autogenerated and should never be modified because every save to EDMX (even after moving some entity in diagram) or T4 template (if you use it) will trigger recreation.
Either move your changes to your own partial classes for your entities or add them directly to code generator - T4 template.

Related

How do I disconnect code generated by a T4 template from that template?

When I 'run' a T4 template, the class files it generates for me all appear under the template. I cannot remove the template from the project without the files going away too. I also can't copy the files out from 'under' the template into the project root because the are already in the project root. How can I emancipate these files from the template that created them?
Get creative:
Right click and open containing folder.
Drag-and-drop the generated file to another location in the solution.
Delete the original template and file from Visual Studio.
Presto.
If you want to do one-time code-generation with T4 then I would suggest using T4 scaffolding. This tool was originally created to support the generation of views, controllers etc. in MVC3 but you can use it on pretty much anything.

Entity Framework - cannot generate database from my EDMX because new entities are not in database?

Having got a chunk of my application working with Entity Framework 4.1, I needed to add a couple more tables. I created the entities and associations in the Model Viewer and then ask it to generate the database from the model.
Immediately I get a selection of validation errors and a pop-up informs me that I must clear all validation errors before I can generate the database.
The errors in question are all along the lines of:
Error 11007: Entity type 'EntityName' is not mapped.
Or
Error 11008: Association 'AssociationName' is not mapped.
I get this error once for each new Entity and Association I have created.
Now I get that they are not mapped to the database yet, this seems quite logical as they are totally new to the model. That is why I'm trying to use the "Generate Database From Model" tools to generate the database.
Am I understanding correctly that I cannot generate a new database from the model because the new Entities and Associations I have added to the Model are not already in the database? If so, how do I work around this?
It transpired that ( as suggested in comments by hdv ) there was another error in the .EDMX file which was being lost amongst the Error Message noise. This related to a field that I had used to key an association and later deleted without realising that the association depended on it.
The error message included a line in the .EDMX file so I closed it in the Model Editor and used Open With > XML Editor to open the source, find the relevant line and delete the association. This flagged up a few more errors where that association was referenced from elsewhere and after removing those I could open the file in Model Viewer again and it allowed me to create the database.
Error 11007: Entity type 'EntityName' is not mapped can also be caused by a left over class file (e.g. entityname.cs) in your solution folder...
I faced this same problem: Error 11007: Entity type 'EntityName' is not mapped. in VS2010 using TFS. I believe it was likely caused by using copy and paste and other edits of new entities in the designer. Even undoing changes in TFS, closing down VS2010 etc. didn't work.
On investigation, even though I had rolled back to the last checked in version in TFS, when viewing the source folder (containing the .edmx file) I discovered an entityname.cs file which related to the offending entity I had been trying to add, had then deleted and recreated. This could also be seen using the 'Show All Files' feature in the VS2010 Solution Explorer.
Once the offending file had been deleted, the EDMX could be edited. Once the database schema has been updated to match, we no longer got the error when using Update Model from Database (which we do in order to reference Custom SQL Functions etc. - somewhat O/T I know).
I hope this helps someone else experiencing the same issue.
Open edmx in xml editor and check if AssociationSetMapping exists for your association.

Is it possible to generate XML from a EDMX model?

I'm generating my POCO's from edmx model, and would like to know if its possible to make use of the neat codegen features of Entity Framework to create my an xml document along with the POCO (which is needed for my project).
You can generate an XML document in Visual Studio using T4 templates.
In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. The generated file can be text of any kind, such as a Web page, or a resource file, or program source code in any language.
But you should note that the EDMX file itself is an XML document.
Right click on your Model.edmx file
'Open With...'
Choose 'XML (Text) Editor'
There are three main sections to the file
StorageModels - describes that database
ConceptualModels - describes your code and objects
Mappings - describes how the two models relate
Look at whether the standard EDMX file contains the type of information you want. If not, parse and shred it using T4 to extract the information you're looking for.
No need to parse the EDMX file, use TiraggoEdmx, you can install it via NuGet. See http://brewdawg.github.io/Tiraggo.Edmx/ it serves up all of the metadata from your EDMX files that Microsoft hides from you, very simple, works great. I wrote it but it's 100% free, even comes with a sample POCO template.

Is it possible to prevent regeneration of T4 files when saving an EDMX?

In Entity Framework 4, when using a T4 template (.tt) for entities generation, saving the .edmx file automatically regenerates all .tt files in the same project. This is a fine behavior by default, since the Entities T4 is added next to the EDMX file.
We moved this template to another project to keep entities separate. We manually regenerate it when a change is made to the EDMX file. However, we have a bunch of other T4 files in the same project as the EDMX, which are automatically regenerated as soon as we save the model.
Is there a way to prevent this behavior? Those files could take a while to regenerate. We could of course move the model to another project on its own, but that's not an option currently.
I don't know how I could have missed this option for months. When nothing is selected in the EDMX designer, there is a property named "Transform Related Text Templates On Save" that can simply be set to false.

Linq to sql as object data source - designer problem with partial classes

I have created a simple LINQ to Sql class item in Visual Studio 2008 (DataClasses1.dbml). I can see three files there: ".layout" file, ".cs" file and ".designer.cs" file.
Next I have added a DataGridView to a Windows Form and selected a new Object Data Source with one table from my LINQ To SQL file. So far everything works OK.
Problem starts when I want to add a new property to one of my auto-generated entity classes. All the classes declared as "partial", so theoretically I should be able to write code to the "DataClasses1.cs" file with my new property. When I do it I can see the new property in my code (Intellisense works) but the designer doesn't recognize it (for example when i select columns in the datagridview it doesn't appear on the list of available fields). But when I move the new property to the "DataClasses1.designer.cs" file everything works fine. I do not want to place my code in designer file as it is autogenerated and can be overwritten autmatically.
What can I do to make the Windows Forms designer recognize properties added to files different than designer.cs ?
Thanks in advance
rclick on dbml and choose View Code (F7). This will create a code file DataClasses1.cs (if its not there already) within the same "group". This file contains a partial class and will never be auto-generated. You can place your code there.

Resources