Entity Framework 4.3 Modify T4 Template for generating classes - poco

I have a database-first model generated with full primary keys. Database first model works fine. However now I have upgraded my Entity Framework 4.3. I have modified the standard T4 template that comes with EF.
Now when I use Add-Migration initial to the project it is complaining that my entities do not have the Primary keys defined. Which is not true (or to some extent)... looking deeper I have composite keys for which EF expects me to define [Key, Column(0)] on all of the key elements.
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'SupplierWarehouses' is based on type 'SupplierWarehouses' that has no keys defined.
The problem I am facing is I trying to modify the WriteProperty method of T4 Template (DbContext) and there seems to be no Entity Key Property on System.Data.Metadata.Edm.EdmProperty object. Now this property is shown from Visual Studio Model Editor so Theory is it should exist.
Appreciate if some one could help me with this
Sanj.

I figured it out System.Data.Metadata.Edm.EdmProperty does not have it this property, it needs to be referred from System.Data.Metadata.Edm.EntityType the actual property is KeyMembers.
I made the change to T4 WriteProperty to accept Entity Object now it works as beauty.

Related

Audit.Net Entity Framework - Independant Associations [Tables Many-To-Many]

Hi i write because i have configured the Audit for one single table for all my entities and its working fine for the general tables in my model, but with the Many-To-Many tables i don't know how can i do for setup the "AssociationEntryRecord"? the event is fired by EF when i do one change in this tables but i don't know how saved!
Could you please help me with this questions, thanks in advance for your help & the library...
For configuring the Entity Framework event provider use the fluent API provided by Audit.EntityFramework.Configuration.Setup()
You can include the associations as follows:
Audit.EntityFramework.Configuration.Setup()
.ForAnyContext(config => config
.IncludeIndependantAssociations());
And about your sample code (what you should have included as textual code and not as an image):
The first line is not needed, since the UseEntityFramework() will
override the DataProvider
The primary key value can be calculated as: entity.TablePk = entry.PrimaryKey.FirstOrDefault().Value.ToString();

Entity Framework 6.x: Customize Fluent API in Code First to an Existing Database

I have installed the EntityFramework.CodeTemplates (as replacement for the previously used Entity Framework Power Tools) and I am struggling to customize the T4 runtime scripts from the files EntityType.cs.t4 and Context.cs.t4 (executed when adding a new "ADO.NET Entity Data Model" to the project and selecting "Code First from Database"). For instance I would like to customize the fluent api code generation and change for instance the property names in the EF entities, to be different from the column names from the mapped tables (which is default).
...
modelBuilder.Entity<Department>()
.Property(t => t.Name)
.HasColumnName("DepartmentName");
...
Another customizations that I would need is to be able to change the entity name (to be different from the mapped table name), or to change the names of the different navigation properties between tables:
...
modelBuilder.Entity<Department>()
.HasMany(e => e.NavigationPropertyName)
...
The default code generation of the fluent api code occurs through the MethodChain() helper methods, called at different parts of the Context.cs.t4 runtime script.
I am stuck with the customization of the t4 scripts, as I did not found any samples how to add some additional fluent api code to the default generated fluent api code in MethodChain(). Can anybody post some samples about how this kind of customizations could be done ?

Doctrine 2 base entities like those in doctrine 1?

I'm starting a new project with Symfony2/Doctrine2 and I generated my entities from the DB with the mapping:convert and mapping:import commands.
My entities are fine, but I only have the generated entities. Back in Doctrine1/Symfony1, you had your entities generated twice : once in the doctrine folder, an almost empty class just extending the second one in doctrine/base folder where all the doctrine internals were (getters/setters, fields...)
This way, you could add your own methods in the first file where they remained untouched even if you generated again the entities (only the doctrine/base files were modified by the generator).
Am I missing something ?
Well the generate command only generates getters and setters that are undefined and appends them to the (entity) file. This means that your custom getters/setters will never be overwritten. Does that answer your question?

Generic comment system in Symfony2

I have in my Symfony 2.1 RC app a simple Comment model (using Doctrine 2). Every comment has a user and a message.
Currently, the CommentBundle manages comments on articles. I'd like it to be more generic to be able to comment any kind of entity without copying code across different bundles dedicated to comments...
For this to work, I also need a way to reference any entity from the comment one. I think having two fields entity_type and entity_id can be a nice solution. However, I can't get the object from these without mapping entity_type to classes manually and using the find method.
So how do I reference an entity from a comment ? And how can I create generic behavior working on several entities ?
You can create a abstract base class entity called Commentable and create entities that inherit Commentable such as Document or Post.
Since Document and Post are derived from Commentable, you can create a one to many relationship between the entities Commentable and Comment respectively.
Make sure to include in your base class ORM annotations for inheritance:
#InheritanceType
#DiscriminatorColumn
#DiscriminatorMap
Examples can be found on Doctrine Project Inheritance Documentation

Shared PKs and ObjectContext errors

I am using EF 4.3 in an ASP.NET WebForms application. I've started with model first approach with context object of type ObjectContext and POCO code generator (via T4).
At the beginning the Context was created at the beginning of every service method. While working on performance I decided to switch to context per web request. Unfortunately I have encountered an issue with Table-per-Type inheritance. I have two entities: Offer and OfferEdit. They are in a one to one relationship, and both share the same primary key. Basically an OfferEdit (OfferEdits table) is created once an Offer is being edited.
I query the context for a particular Offer entity more then once during web request. The error I get trying to execute:
var offer = Context.Offer.Where(o => o.Id == offerId).FirstOrDefault()
when this offer is already loaded to Context.Offer.EntitySet is
All objects in the EntitySet 'RuchEntities.Offer' must have unique primary keys.
However, an instance of type 'Ruch.Data.Model.OfferEdit' and an instance of type'Ruch.Data.Model.Offer' both have the same primary key
value,'EntitySet=Offer;Id=4139'.
Will appreciate all advice.
Sounds like you are misusing TPT inheritance. To make it clear EF inheritance works exactly same way as in .NET - the entity can be either of type Offer or OfferEdit. You can convert OfferEdit to Offer but it is still OfferEdit. It can never be of both types which means you can never have Offer entity with Id same as OfferEdit entity because same key cannot be used by two entity instances. You also never can change instance of Offer to OfferEdit because .NET doesn't allow you changing type of existing instance.

Resources