Creating entities from template

Posts   
 
    
Expert-IT
User
Posts: 5
Joined: 09-Apr-2013
# Posted on: 24-Apr-2013 10:55:34   

Hello,

Is there a way to create an entity starting from a template ? We need in every entities a set of fields (like CreatedAt, DeletedAt, ...), we don't want to use inheritance because conceptually it's not the case.

Thanks in advance

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 24-Apr-2013 16:16:02   

You're using v4 or other? In v4, you can create a valuetype with the fields, map it in the entities as a single field and use field inlining at generation time. Not sure if it solves your problem as such.

Frans Bouma | Lead developer LLBLGen Pro
Expert-IT
User
Posts: 5
Joined: 09-Apr-2013
# Posted on: 25-Apr-2013 10:37:22   

Thanks a lot Otis, it works fine except that in the generated code, the fields are not directly members of my entity, but members of the valuetype class generated. Maybe the solution is "use field inlining at generation time", but I don't see what you mean by that ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 25-Apr-2013 10:39:28   

Expert-IT wrote:

Thanks a lot Otis, it works fine except that in the generated code, the fields are not directly members of my entity, but members of the valuetype class generated. Maybe the solution is "use field inlining at generation time", but I don't see what you mean by that ?

What target framework do you use? Because field inlineing is only available for our own framework. Other frameworks don't have the field inlining as they (except linq to sql) support value types.

Frans Bouma | Lead developer LLBLGen Pro
Expert-IT
User
Posts: 5
Joined: 09-Apr-2013
# Posted on: 25-Apr-2013 12:23:54   

In fact we are using Entity Framework, as target FWK, so ...

but anyway with valuetype, it's a start, maybe by adapting the generation templates, we will have a solution tailored to our needs.

Thanks, Albert

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 26-Apr-2013 11:51:20   

the fields in an entity have to be mapped in the EDMX file, so they have to be mapped before code generation starts.

You could do the following: abuse the element search feature. It'll execute all code you enter over the model, so you can do the following (in a search for entities for example) (the code below is sloppy, it's only for illustration purposes.

  • for each entity in the model
  • if it doesn't have a field called 'CreatedBy'
  • do : var f= e.Fields.AddNew(); f.Name = 'CreatedBy'; f.FieldType = p.TypeShortcuts.FirstOrDefault(t=>t.Shortcut=="string");
  • do this also for the other fields you want.

It might be wise to do this inside a non-undoable period:

CommandQueueManagerSingleton.GetInstance().BeginNonUndoablePeriod();
var saveRaiseEvents = CommandQueueManagerSingleton.GetInstance().RaiseEvents;
CommandQueueManagerSingleton.GetInstance().RaiseEvents = false;
try
{
// your code
}
finally
{
    CommandQueueManagerSingleton.GetInstance().RaiseEvents = saveRaiseEvents;
    CommandQueueManagerSingleton.GetInstance().EndNonUndoablePeriod();
}

and then at the end return an object to satisfy the search.

You can also create a simple plugin which does this.

After the fields are added, right click 'entities' in project explorer and execute the 'automap unmapped fields ' feature to create table fields if necessary and create the proper mappings.

Frans Bouma | Lead developer LLBLGen Pro