Page 1 of 1
Update edml from Template
Posted: Mon 05 Sep 2011 10:33
by delarou
After creating a model from database, we would like to create a script (template) that does some default transformations on the model.
For example, renaming all primary properties to the name 'Id'. Is this possible? When I try for example something like this I get an exception
Posted: Thu 08 Sep 2011 10:20
by Shalex
delarou wrote:When I try for example something like this I get an exception
If you want to generate the file with the list of your entity classes names with added "SomeSuffix", please use this code:
Specify the exact exceptions you are getting if any.
delarou wrote:For example, renaming all primary properties to the name 'Id'. Is this possible?
You should create your own template by modifying our predifined template: navigate to the Tools > Entity Developer > Model Explorer menu of Visual Studio, right click on the predefined template > Copy to Model Folder, and make these changes:
a) replace
' property.Name' with
' property.PrimaryKey ? "Id" : property.Name' (do not modify relationProperty.Name)
and
b) replace
'(property.Name' with
'(property.PrimaryKey ? "Id" : property.Name'.
Save the model and generate the code.
Disadvantages of this approach:
1) if your table in the database has more than one primary key column, the modified template will generate invalid code;
2) when you upgrade our provider, only predifined templates will be upgraded (not your own).
For more information, please refer to the documentation:
- download
http://www.devart.com/entitydeveloper/e ... eloper.chm and open the Common Concepts > Working with Templates section;
- general T4 reference, e.g.
http://msdn.microsoft.com/en-us/library/bb126478.aspx.
Posted: Tue 27 Sep 2011 09:17
by delarou
In my database I have tables with 4 audit fields (CreatedBy, ModifiedBy, CreationDate, ModifiedDate). On the model I want to migrate these 4 fields
programmatically (template) to a ComplexType (called AuditInfo).
I wrote something like
Code: Select all
EntityDeveloper.Mapping.IDbColumn column = GetColumnByName(cls, "DATUM_CREATIE");
EntityDeveloper.Mapping.ColumnMapping columnMapping = new EntityDeveloper.Mapping.ColumnMapping(column, dateCreationProperty);
cls.ClassMapping.TableMappings[0].ColumnMappings.Add(columnMapping);
the column is ok but I can't find the right code to define the dateCreationProperty.
In other words, what is the code behind the migration wizard to a complex type through the UI
Christoph
Posted: Wed 28 Sep 2011 09:06
by Shalex
If you want to implement generation of complex types, you should code these steps:
1) create a new complex type with the properties which correspond to CreatedBy, ModifiedBy, CreationDate, ModifiedDate;
2) find the classes (which include CreatedBy, ModifiedBy, CreationDate, ModifiedDate) in your model and replace these 4 properties by one complex type property;
3) set mapping.
What have you already implemented? Please specify the problem more exactly.
Posted: Thu 13 Oct 2011 12:03
by delarou
1) And 2) is not a problem.
The problem is to find the (sub)properties of complex property to do the mapping with.
I wrote something like:
EntityDeveloper.Mapping.ColumnMapping cMap = new EntityDeveloper.Mapping.ColumnMapping(GetColumnByName(cls, "DATUM_CREATIE"), prop);
cls.ClassMapping.TableMappings[0].ColumnMappings.Add(cMap);
The GetColumnByName() works fine but how to define the prop?
I can say:
EntityProperty complexProp = GetPropByName(cls, "Audit");
Which gives me the complexProperty “Audit”, consisting of 4 (sub)properties.
But how(where) to find these (sub)properties of a complextype (of a given table) f.e. the Audit.DateCreation, Audit.UserCreation,… fields, as you can see them in the ‘Entity mapping details’ interface?

Posted: Mon 24 Oct 2011 13:44
by Shalex
Assuming that there is Class1 with Property1, Property2, Property3 in your model, and you want to create a complex type basing on Property2 and Property3, please use the code below.
This works for moving properties with their mapping into complex type, but it doesn't work for more complecated case when one of properties, which are moved to the complex type, is of complex type itself.
sourceClass - a class, from which we are moving properties
destComplexType - a new complex type
newParentProperty - a new complex type property (it will be created in sourceClass)
properties - the properties to migrate
Each property for migration is copied (not moved) into the complex type, destProperty is a copy ot the property in the complex type.
Code: Select all
c.Name == "Class1");
Property newParentProperty = model.CreateNewProperty();
newParentProperty.Name = "MyParentProperty";
newParentProperty.Type = destComplexType;
sourceClass.AddProperty(newParentProperty);
foreach (Property baseProperty in sourceClass.Properties.Where(p => p.Name == "Property2" || p.Name == "Property3").ToList()) {
Property destProperty = baseProperty.Clone() as Property;
destProperty.ParentClass = destComplexType;
destProperty.Name = baseProperty.Name;
destComplexType.AddProperty(destProperty);
foreach (EntityDeveloper.EntityFramework.Mapping.EntityTableMapping tableMap in sourceClass.ClassMapping.TableMappings) {
for (int i = tableMap.ColumnMappings.Count - 1; i >= 0; i--)
if (tableMap.ColumnMappings[i].Property == baseProperty) {
EntityDeveloper.Mapping.ColumnMapping m = new EntityDeveloper.Mapping.ColumnMapping(tableMap.ColumnMappings[i].Column, destProperty);
foreach (Property p in tableMap.ColumnMappings[i].ParentProperties)
m.ParentProperties.Add(p);
m.ParentProperties.Add(newParentProperty);
tableMap.ColumnMappings.Remove(tableMap.ColumnMappings[i]);
tableMap.ColumnMappings.Add(m);
}
sourceClass.RemoveProperty(baseProperty);
}
}
#>