Oh and for sharings sake, heres my 'models' template I use with mvc apps. It does assign a few custom attributes though but they can be swapped for native data annotations or removed completely.
<[ System.Text.RegularExpressions ]><%
string rns = _executingGenerator.RootNamespaceToUse;
EntityDefinition ed = _activeObject as EntityDefinition;
var edMappings = _executingGenerator.ProjectDefinition.GetGroupableModelElementMapping(ed, _executingGenerator.DriverID);
var rels = _executingGenerator.ProjectDefinition.GetAllRelationshipsForEntity(ed, true);
%><~
string GetCSharpTypeName(Type type)
{
string name = type.FullName;
switch (name)
{
case "System.String" :
return "string";
case "System.Int16" :
return "short";
case "System.Int32" :
return "int";
case "System.Int64" :
return "long";
case "System.Decimal" :
return "decimal";
case "System.Single" :
return "float";
case "System.Double" :
return "double";
case "System.SByte" :
return "sbyte";
case "System.Byte" :
return "byte";
case "System.Boolean" :
return "bool";
}
return name;
}
bool IsPartOfUpdateable(FieldElement field)
{
var checkFor = new [] {"UpdatedBy", "UpdatedOn", "CreatedBy", "CreatedOn" };
return checkFor.Contains(field.Name);
}
public class ModelFieldAttribute
{
public ModelFieldAttribute(){ ConstructorArguments = new List<string>(); }
public string Name{ get; set; }
public List<string> ConstructorArguments { get;set; }
}
public class AttributeSet : List<ModelFieldAttribute>
{
public string Render()
{
List<string> bits = new List<string>();
foreach (var attr in this)
{
string attribute = attr.Name;
if (attr.ConstructorArguments.Count > 0)
attribute += "(" + string.Join(", ", attr.ConstructorArguments.ToArray()) + ")";
bits.Add(attribute);
}
if (bits.Count() == 0)
return null;
return string.Format("[{0}]{1}\t", string.Join(", ", bits.ToArray()), Environment.NewLine);
}
}
string CreateFriendlyName(string name){
Regex acronym = new Regex("^[A-Z0-9]+(Id)?$");
Regex exp = new Regex("(\\w)([A-Z])"),
idExp = new Regex("(\\w)Id$");
if (idExp.IsMatch(name))
{
if (acronym.IsMatch(name))
return idExp.Replace(name, "$1");
return exp.Replace(idExp.Replace(name, "$1"), "$1 $2");
}
if (acronym.IsMatch(name))
return name;
return exp.Replace(name, "$1 $2");
}
~>
using System.Web.Mvc;
using Shivam.ASL.Core.Model.Validators;
using Shivam.ASL.Core.Model;
using System.ComponentModel.DataAnnotations;
namespace <%= rns %>.Web.Models
{
#region Add Model
<% foreach (var f in ed.Fields){
if (IsPartOfUpdateable(f))
continue;
if (f.IsPartOfIdentifyingFields)
continue;
var targetField = edMappings.GetFieldMappingOfField(f).MappedTarget;
string typeName = GetCSharpTypeName(f.FieldType.RepresentedType);
string dbType = targetField.DbTypeAsString;
AttributeSet aset = new AttributeSet();
if (!f.IsOptional && typeName != "bool")
aset.Add(new ModelFieldAttribute() {Name="RequiredField"});
if (f.IsForeignKeyField)
aset.Add(new ModelFieldAttribute() {Name="UIHint", ConstructorArguments = {@"""SelectList"""}});
if (dbType == "ntext")
aset.Add(new ModelFieldAttribute() {Name="CKEditor"});
else{
if (typeName == "string"){
aset.Add(new ModelFieldAttribute() {Name="Length", ConstructorArguments = {f.MaxLength.ToString()}});
if (f.MaxLength >= 256)
aset.Add(new ModelFieldAttribute() {Name="DataType", ConstructorArguments = {"DataType.MultilineText"}});
}
}
if (typeName == "System.DateTime")
aset.Add(new ModelFieldAttribute() {Name="DataType", ConstructorArguments = {"DataType.Date"}});
if (typeName != "string" && f.IsOptional)
typeName += "?";
string fieldName = f.Name;
string friendlyName = CreateFriendlyName(fieldName);
if (fieldName != friendlyName)
aset.Add(new ModelFieldAttribute() {Name="DisplayName", ConstructorArguments = {@"""" + friendlyName + @""""}});
%>
<%= aset.Render()%>public <%= typeName %> <%= f.Name %>
{
get;
set;
}
<% }
foreach(var rel in rels){
if (!(rel.RelationshipType == EntityRelationshipType.ManyToMany))
continue;
var mmRel = rel as IndirectRelationshipEdge;
AttributeSet aset = new AttributeSet();
aset.Add(new ModelFieldAttribute() {Name="DisplayName", ConstructorArguments = {@"""" + CreateFriendlyName(mmRel.SecondRelationship.EntityPkSide.Name + "s") + @""""}});
%>
<%= aset.Render()%>public int[] <%= rel.StartEntityNavigator %>
{
get;
set;
}
<%
}
%>
#endregion
#region Edit Model
<% foreach (var f in ed.Fields){
var targetField = edMappings.GetFieldMappingOfField(f).MappedTarget;
string typeName = GetCSharpTypeName(f.FieldType.RepresentedType);
string dbType = targetField.DbTypeAsString;
AttributeSet aset = new AttributeSet();
if (!f.IsOptional && typeName != "bool" && !IsPartOfUpdateable(f))
aset.Add(new ModelFieldAttribute() {Name="RequiredField"});
if (f.IsPartOfIdentifyingFields){
aset.Add(new ModelFieldAttribute() {Name="HiddenInput", ConstructorArguments = {"DisplayValue = false"}});
}
else{
if (IsPartOfUpdateable(f))
aset.Add(new ModelFieldAttribute() {Name="ReadOnly", ConstructorArguments = {"true"}});
if (f.IsForeignKeyField)
aset.Add(new ModelFieldAttribute() {Name="UIHint", ConstructorArguments = {@"""SelectList"""}});
if (dbType == "ntext")
aset.Add(new ModelFieldAttribute() {Name="CKEditor"});
else{
if (typeName == "string"){
aset.Add(new ModelFieldAttribute() {Name="Length", ConstructorArguments = {f.MaxLength.ToString()}});
if (f.MaxLength >= 256)
aset.Add(new ModelFieldAttribute() {Name="DataType", ConstructorArguments = {"DataType.MultilineText"}});
}
}
if (typeName == "System.DateTime")
aset.Add(new ModelFieldAttribute() {Name="DataType", ConstructorArguments = {"DataType.Date"}});
if (typeName != "string" && f.IsOptional)
typeName += "?";
}
string fieldName = f.Name;
string friendlyName = CreateFriendlyName(fieldName);
if (fieldName != friendlyName)
aset.Add(new ModelFieldAttribute() {Name="DisplayName", ConstructorArguments = {@"""" + friendlyName + @""""}});
%>
<%= aset.Render()%>public <%= typeName %> <%= f.Name %>
{
get;
set;
}
<% }
foreach(var rel in rels){
if (!(rel.RelationshipType == EntityRelationshipType.ManyToMany))
continue;
var mmRel = rel as IndirectRelationshipEdge;
AttributeSet aset = new AttributeSet();
aset.Add(new ModelFieldAttribute() {Name="DisplayName", ConstructorArguments = {@"""" + CreateFriendlyName(mmRel.SecondRelationship.EntityPkSide.Name + "s") + @""""}});
%>
<%= aset.Render()%>public int[] <%= rel.StartEntityNavigator %>
{
get;
set;
}
<%
}
%>
#endregion
#region List Model
<% foreach (var f in ed.Fields){
var targetField = edMappings.GetFieldMappingOfField(f).MappedTarget;
string typeName = GetCSharpTypeName(f.FieldType.RepresentedType);
string dbType = targetField.DbTypeAsString;
AttributeSet aset = new AttributeSet();
string fieldName = f.Name;
string friendlyName = CreateFriendlyName(fieldName);
if (fieldName != friendlyName)
aset.Add(new ModelFieldAttribute() {Name="DisplayName", ConstructorArguments = {@"""" + friendlyName + @""""}});
if (typeName != "string" && f.IsOptional)
typeName += "?";
%>
<%= aset.Render()%>public <%= typeName %> <%= f.Name %>
{
get;
set;
}
<% }
foreach(var rel in rels){
if (!(rel.RelationshipType == EntityRelationshipType.ManyToMany))
continue;
var mmRel = rel as IndirectRelationshipEdge;
AttributeSet aset = new AttributeSet();
aset.Add(new ModelFieldAttribute() {Name="DisplayName", ConstructorArguments = {@"""" + CreateFriendlyName(mmRel.SecondRelationship.EntityPkSide.Name + "s") + @""""}});
%>
<%= aset.Render()%>public string[] <%= rel.StartEntityNavigator %>
{
get;
set;
}
<%
}
%>
#endregion
}