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.
Related
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.
I am working with perl Catalyst. I created Model and Schema for a database with model name MyDBI.pm and schema for that are created in SchemaClass/Result directory. Now I have added a new table in my database. Now I need to create a Schema for that table only. The following command I am using for creating Schema
perl script/my_create.pl model MyDBI DBIC::Schema
My::SchemaClass::Result::Tablename create=dynamic
components=TimeStamp 'dbi:Pg:dbname=mydb' username 123 '{ AutoCommit => 1}'
But it creating MyDBI.pm again as MyDBI.pm.new. So how can I create without creating that MyDBI.pm.
Thanks In Advance....
As long as you edit the classes under Schema/Result/.pm classes only under the marked line, there is no problem it re-creates it? That way you know your model is always up-to-date.
The class in models (MyDBI.pm) only contains the connection info, right?
In the Schema classes, you find the line marked by something like this:
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+ZwJisOqZAkKLq170l/CNA
It is much more common and in my opinion much cleaner to add Schema classes not directly into Result.pm, which will get cluttered,
but to the Schema/Result/ directory, for example
..../Schema/Result/New.pm -> Schema::Result::New
Then use base qw/DBIx::Class::Core/; in the package and all is set to be used normally.
Catalyst will discover those and set them up for you automatically.
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
{
}
I want to add Entity Framework 5 database first into a class library in Visual Studio 2012 targeting .net framework 4.5. I am confused with all the labels I need to key in:
The EDMX file name when adding ADO.NET entity data model to the project. I put ‘MyEF.edmx’.
When saving the connection string into the config file. I put ‘MyEntities’.
After selecting some tables to include in my model, there’s a textbox to input model namespace. I put ‘MyModel’.
Property ‘custom tool namespace’ of the MyEF.edmx file. I put ‘TheEF’.
Property ‘custom tool namespace’ of the MyEF.Context.tt file. I put ‘TheContext’.
Property ‘custom tool namespace’ of the MyEF.tt file. I put ‘TheModel’.
Opening MyEF.edmx with ADO.NET entity data model designer, looking at the properties of MyModel, there are:
entity container name, filled with ‘MyEntities’. So the connection string name goes here.
namespace, filled with ‘MyModel’. This is coming from the table selection textbox.
Putting something into the edmx custom tool namespace doesn’t seem to do anything. I got this conclusion because when I grep the entire source code folders, I found it only in a vbproj file.
Putting ‘TheModel’ into MyEF.tt custom tool namespace produces error from MyEF.Context.vb saying type ‘MyTable’ (this is the name of my database table) is not defined.
Can someone explain the purpose of each label?
If I want to put all the classes generated by this one edmx (DbContext, models, etc.) into one namespace, ‘MyEF’, what should I put in each of those places?
The various properties are used as follows:
EDMX file name --> Used for the EDMX file name
Connection string name --> Used for the connection string name in the config file, and also for the container name of the conceptual model (CSDL) part of the EDMX
Model namespace --> Used for the namespace of the conceptual model (CSDL) part of the EDMX, and also for the store model (SSDL) part with .Store appended
Custom tool namespace for the EDMX file --> I don't believe this is used for anything when using T4 generation of POCO entities. When using EF1-style built in code generation, setting this property will set the .NET namespace for all generated files.
Custom tool namespace for .Context.tt file --> The .NET namespace used in the source file for the context
Custom tool namespace for .tt file --> The .NET namespace used in the source files for the entities
Note that if you set the .Context.tt and .tt custom namespaces to different things, then the context will be generated in a different namespace to the entity types and this won't compile out-of-the-box. You can update the .tt files if you want to use different namespaces here, but more often people just use the same namespace for both.
Also note that you may need to choose "Run Custom Tool" from the context menu for each .tt file after changing the properties in order for the code to be re-generated.
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.