How to Create Custom Partial CLasses in VS 2012 EF Model - frameworks

Just started using VS 2012 and I generated an EF Model from the database. All worked fine, although in my previous experience I had to put a using in my code.
I tried to set up a .cs file with partial classes to provide custom business logic behaviors to the generated classes and Intellisense does not recognize ar prompt with the generated classes.
I did as others have stated and brought up the properties panel of the Edmx model and fount the namespace. However, when I try to put it in a using statement, Intellisense does not recognize it.
Am I missing something here? EF can get very confusing and frustrating if one goes more than an inch or so below its generated surface.

Turns out that EF5 generates a model such that you do not need to have a "using namespace;" or a "namespace xxx"{} in the code file.
Just a simple xxx.cs file and each partial class definition you want to make:
public partial class MyClass
{
}

Related

EF6.Utility.CS.ttinclude: ArgumentNotNull does not exist

I just upgraded to EF6 from EF5 and I encounter this error in a custom T4 that connects to the DB using a DbContext from a different assembly.
File: EF6.Utility.CS.ttinclude
Compiling transformation: The name 'ArgumentNotNull' does not exist in the current context
What I've done is replacing EF.Utility.CS.ttinclude with EF6.Utility.CS.ttinclude, which solved another error about DbSet and DbContext not being found.
The T4 is very simple, like this one:
using(var context = new EntityContext)
return context.Entities.Where(x => 1==1);
Except for the EF include I only reference my own assemblies. The freshly-added Context is generating just fine (in another project).
What on earth could be wrong?
EF6.Utility.CS.ttinclude reference some static functions that are defined in the main template.tt so you need to have them in your template too.
Example:
Entity.tt defines ArgumentNotNull(T arg, string name) which is used in the EF6.Utility.CS.ttinclude (it's not the way we are used to have)
See the bottom of generated template from EF6 designer to see this missing functions
One way to solve this is moving almost all the code to a .cs file and then use that file in the T4 template. Then remove the EF ttinclude from the T4 template.
Works and is easy better praxis to follow.

How do I stop Entity Framework from generating BOTH ObjectContext and dbContext

Background
Visual Studio 2012
NEW Model.EDMX file created in VS2012
Copied some of the EDMX xml from a previously created EDMX into the new one
Problem / Question
Now. The EDMX (TT transform, custom tool, whatever, etc.) is generating BOTH DbContext classes (under the Model.tt/Model.Context.tt files) and ObjectContext classes (via the Model.designer.cs file). See image below:
Everything builds fine and works with the DbContext (but obviously only if I delete the Designer.cs file just before building) but the Designer.cs - and its ObjectContext-based code - keeps reappearing. How do I stop this behavior?!
I didn't find out how to stop the Designer.cs file from generating the ObjectContext, but I did figure out how to make it so that it doesn't matter. Just set the Build Action to "None" instead of "Compile".
The .tt files will generate the code regardless of the code generation strategy in the .edmx. They listen to the .edmx file changes. At least this is how they are working for me.
So by turning the code generation strategy to None in the .edmx you make the .designer.cs file empty of any useful content.
Then open up the project file, find the nodes representing the .edmx, by default it is contained in EntityDeploy node ie the Build Action value, and remove its Generator subkey.
On normal EF Code First projects, Code Generation Strategy is "None" and ObjectContext is not generated.
It appears in your case that you Code Generation Strategy set to "Default".
To stop generating ObjectContext in xxxx.Designer.cs, go to your edmx file, and change your Code Generation Strategy from "Default" to "None".
If you inspect xxxx.Designer.cs
// Default code generation is disabled for model 'C:\Users\xyxy\xyxyxy.Web\Models\xyxy.edmx'.
// To enable default code generation, change the value of the 'Code Generation Strategy' designer
// property to an alternate value. This property is available in the Properties Window when the model is
// open in the designer.
I think the issue is having an EntityModelCodeGenerator value in the Custom Tool field of the .edmx file properties. Just delete that value.
I think this is it, because I notice that in the project that I started off as an EF6 project, there is no value and it has no .designer.cs file, but in the two that I upgraded from EF4, they both have the value and the designer file.
(This equates to a <Generator> tag in the underlying .csproj file.)

Howto customize pluralisation for Entity Framework 5

As my database was designed using german table- and column names, the default pluralisation feature of entity framework doesn't work for me.
I have found a couple of resources where this is discussed, but none of them seem to work.
What i have found:
There is the PluralisationService where i can add the mappings:
PluralizationService pluralizer =
PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
ICustomPluralizationMapping mapping = ps as ICustomPluralizationMapping;
mapping.AddWord("Tabelle", "Tabellen");
But what's next?
I have tried to:
EntityModelSchemaGenerator generator = new EntityModelSchemaGenerator(container);
generator.PluralizationService = pluralizer;
generator.GenerateMetadata();
and put both of them in my POCO T4 Template. But it throwed the following exception:
The EntityContainer 'ContainerName' is not a store EntityContainer. Parameter name: storeEntityContainer
at System.Data.Entity.Design.EntityModelSchemaGenerator.Initialize(...)
at Microsoft.VisualStudio.TextTemplating...GeneratedTextTransformation.TransformText()
To completely customize the table names in EF Code First, you can use the Table attribute to explicitly specify the name of the table associated with a class:
[Table("InternalBlogs")]
public class Blog
{
//...
}
I'm also looking for the same thing. Maybe this can help. I'm just not willing to pay for such a basic feature.
EDIT:
The code you posted is to be used with EdmGen2 wich will give you CSDL, SSDL or MSL files pluralized according to your class.
A very old question, but if someone is still looking for a possible workflow/solution:
I had a similar problem where I wanted to customize the schema import (CSDL) from the database. The solution / workflow was as follows:
Deployed the database schema (I used Visual Studio Database Project VS 201x) to a
local database
Imported the database model using EDMGEN to create the CSDL, SSDL and MSDL files
http://msdn.microsoft.com/en-us/library/vstudio/bb387165(v=vs.110).aspx
Modified EDMGEN2 with my changes on how to handle pluralization and naming with custom rules and created EDMX file
Ran the T4 templates (with additional customization as needed) to create
output.

iPhone Objective-C Error from Files Generated from Data Model

I created a data model in Xcode with about 9 entities, all of which are connected via relationships to a single 'core' entity.
Once I set this data model up graphically using the "Graph Editor Style" for the xcdatamodelId, I generated the class files by going to Editor -> Create NSManagedObject Subclass. I did this twice to establish all classes so there were no properties of type "NSManagedObject".
However without touching any of the generated files, I am getting 4 errors for one of my class entities (Style), all of which are:
Semantic Issue - Redefinition of 'Style' as a different kind of symbol.
Considering these files are generated, I'm more than confused as to what I need to do to fix these. The relationships between entities are straightforward, and there is nothing special about the 'Style' entity which differentiates it from the rest.
Is this possible due to the forward declarations in the various files? I'm stumped as how to rectify this - and I'm a beginner with iPhone development so I'm not quite sure how to approach this. Help is greatly appreciated!
You cannot create a class named Style
it is already defined on MacTypes.h
You should always prefix your classes with your own namespace i.e. Style should be DWStyle

CA: Suppress results from generated code not working in VS2010 beta 2

I'm trying to run codeanalysis on an assembly that contains an entity model (edmx file). In project properties I have checked the "Suppress results from generated code" option, but I am still getting a lot of CA errors pertaining to the auto-generated EF code.
Has anyone experienced this? And is there a work-around?
Just put the attribute on your class definition.
But how to do it, since your file can get overridden any time. Use a separate file, since all generated classes are partial classes. Open a separate file, and write something like:
[GeneratedCode("EntityModelCodeGenerator", "4.0.0.0")]
public partial class YourEntitiesContextName : ObjectContext
{
}
This will skip code analysis on your particular generated class. StyleCop for instance is more smart and doesn't touch files that have .designer/.generated part in their name or regions that have generated word in their name.
Well, "Suppress results from generated code" really means "Don't look at types with GeneratedCodeAttribute". EF's code generator hasn't added this, historically (though I've suggested it to the team). But you can add it if you use custom T4.