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

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.

Related

.NET Entity Framework 4.0 - prevent overwrite

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.

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.

Best way to deploy and reference an XSLT file

In a visual studio project I have three layers, Data Layer, Business Layer and Presentation Layer.
In the Data Layer I have a few XSLT's that transform some objects into an email, all works fine but I have discovered that the XSLTs do not get built/copied when building.
I have currently, created a folder in the deploy location and placed the XSLT's there but I am concerned about relying on a manual process to update these.
Has anyone encountered a similar issue and if so how did they get around it.
It smacks of changing the MSBuild script to copy the build artifacts to the required location, does anyone have examples of this?
Thaks
If you are using Visual Studio 2005/2008, the easiest way to do this is by including your XSLT files as project resources.
Open the Properties for your project.
Select the Resources tab. You will probably see a link that says "This project does not contain a default resources file. Click here to create one." Go ahead and click on that.
Click the Add Resource drop-down near the top and select Add Existing File.
Browse to your XSLT files and select them.
After you have done this, you can easily access the resources in the following manner:
// To get the contents of the resource as a string:
string xslt = global::MyNamespace.Properties.Resources.MyXsltFile;
// To get a Stream containing the resource:
Stream xsltStream = global::MyNamespace.Properties.Resources.ResourceManager.GetStream("MyXsltFile");
If you are using Visual Studio 2003, your best bet is to include those XSLT files as embedded resources for the DLL. In Visual Studio, select the file(s) in Solution Explorer, open the Properties pane, and change the Build Type to "Embedded Resource". You can then use the GetManifestResourceStream method to get a Stream containing the XSLT(s). The name to pass will be based on the default namespace of your assembly, the folder containing the file, and the name of the file.
For example, say your data layer assembly has a default namespace of My.DataLayer. Within your data layer project you have a folder named Templates which contains a file called Transform.xslt. The code to get your XSLT would look like this:
// There are numerous ways to get a reference to the Assembly ... this way works
// when called from a class that is in your data layer. Have a look also at the
// static methods available on the Assembly class.
System.Reflection.Assembly assembly = (GetType()).Assembly;
System.IO.Stream xsltStream = assembly.GetManifestResourceStream("My.DataLayer.Templates.Transform.xslt");
For more information check out this article on CodeProject.
Obvious question maybe, but still has to be asked, did you include the folder containing the XSLT's in the project itself? Is this a web or forms app?
In VS, it is easy to set the properties of the XSLT files in the project to copy on build, by default they do not.
I may have explained myself poorly.
THe Data layer is a class library that a the presentation layer references.
On building the DataLayer I can get the XSLTs to output to the Bin directory of the DataLayer. However when I build and publish the presentation layer, it correctly grabs the DLL but not the XSLTs

Resources