Paste the following block of code into the file, replacing everything:
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using
System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
namespace EntityGenerator
{
public partial class GenerateEntities
{
#region Private members
private IOrganizationService
_service;
private string _prefix;
private PluralizationService
_pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
#endregion
#region Constructor
public GenerateEntities(string org, string prefix, string userName, string userPassword, string domainName, string serverUrl )
{
//SET
UP CREDENTIALS
Uri OrganizationUri = new Uri(serverUrl + "/" + org + "/XRMServices/2011/Organization.svc");
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential(userName,
userPassword, domainName);
//INITIALIZE
PIPELINE SERVICE
OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, credentials, null);
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
IOrganizationService service = (IOrganizationService)serviceProxy;
_service = service;
_prefix
= prefix;
}
#endregion
#region General private methods
private void
CreateEntity(IOrganizationService service, EntityMetadata emd,
StringAttributeMetadata sam)
{
CreateEntityRequest cr = new CreateEntityRequest();
cr.Entity = emd;
cr.PrimaryAttribute = sam;
service.Execute(cr);
}
private void
CreateAttribute(IOrganizationService service, string parentEntity, AttributeMetadata amd)
{
CreateAttributeRequest car = new CreateAttributeRequest();
car.EntityName = parentEntity.ToLower();
car.Attribute = amd;
service.Execute(car);
}
#endregion
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"../MyEntityDataModel.edmx";
EdmItemCollection ItemCollection =
loader.CreateEdmItemCollection(inputFile);
EntityFrameworkTemplateFileManager
fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
public void CreateAllEntities()
{
string entityName;
string entityNamePlural;
EntityMetadata
emd;
StringAttributeMetadata
sam;
AttributeMetadata
amd;
MemoAttributeMetadata
mam;
<#
foreach (EntityType entity in
ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
string firstLetter =
code.Escape(entity).Substring(0,1).ToLower();
string entityName =
code.Escape(entity);#>
entityName
="<#=entityName#>";
entityNamePlural=_pluralizer.Pluralize(entityName);
//=====================================================================================================
//Create
the <#=entityName#> entity
emd = new EntityMetadata();
emd.SchemaName = _prefix + entityName;
emd.DisplayName = new Microsoft.Xrm.Sdk.Label(entityName, 1033);
emd.DisplayCollectionName
= new
Microsoft.Xrm.Sdk.Label(entityNamePlural, 1033);
emd.OwnershipType = OwnershipTypes.UserOwned;
emd.IsActivity = false;
sam = new StringAttributeMetadata();
sam.SchemaName = _prefix + entityName + "Name";
sam.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended);
sam.MaxLength = 100;
sam.Format = Microsoft.Xrm.Sdk.Metadata.StringFormat.Text;
sam.DisplayName = new Microsoft.Xrm.Sdk.Label("<#=entityName#>Name", 1033);
//Make the WCF call
to generate the entity
CreateEntity(_service, emd, sam);
//Create the
attributes for <#=entityName#>
<#foreach (EdmProperty
edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType
&& p.DeclaringType == entity))
{
string
dataType=code.Escape(edmProperty.TypeUsage);
string
fieldName=edmProperty.ToString();
if(fieldName.ToLower().Trim()==entityName.ToLower()+"id" )
{
fieldName=entityName.ToLower()+"_id" ;
}
string cleanDataType;
bool isNullable =
dataType.IndexOf("Nullable")==-1?false:true;
bool isSupported=false;
cleanDataType=
dataType.Replace("Nullable","");
cleanDataType=
cleanDataType.Replace("System.","");
cleanDataType=
cleanDataType.Replace("Offset","");
cleanDataType=
cleanDataType.Replace("<","");
cleanDataType=
cleanDataType.Replace(">","");
cleanDataType=
cleanDataType.Replace("[","");
cleanDataType=
cleanDataType.Replace("]","");
#>
<#if(fieldName.ToLower().Trim()!=entityName.ToLower()+"id" &&
fieldName.ToLower().Trim()!=entityName.ToLower()+"name"){#>
//attribute name is <#= fieldName
#> of
type <#=dataType#>, isNullable = <#= isNullable
#>
and clean type = <#= cleanDataType #>
<#
switch (cleanDataType)
{
case "int":
#>amd
= new
IntegerAttributeMetadata();
amd.SchemaName = _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
amd.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
amd.DisplayName = new
Microsoft.Xrm.Sdk.Label("<#= fieldName #>", 1033);
amd.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, amd);
<#
break;
case "DateTime":
#>amd
= new
DateTimeAttributeMetadata(DateTimeFormat.DateAndTime);
amd.SchemaName = _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
amd.DisplayName = new
Microsoft.Xrm.Sdk.Label("<#= fieldName #>", 1033);
amd.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, amd);
<#
break;
case "decimal":
#>amd
= new
DecimalAttributeMetadata();
amd.SchemaName = _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
amd.DisplayName = new Microsoft.Xrm.Sdk.Label("<#= fieldName
#>", 1033);
amd.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, amd);
<#
break;
case "byte":
#>amd
= new
IntegerAttributeMetadata();
amd.SchemaName = _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
amd.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
amd.DisplayName = new
Microsoft.Xrm.Sdk.Label("<#= fieldName #>", 1033);
amd.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, amd);
<#
break;
break;
case "bool":
#>amd
= new
DecimalAttributeMetadata();
amd.SchemaName = _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
amd.DisplayName = new
Microsoft.Xrm.Sdk.Label("<#= fieldName #>", 1033);
amd.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, amd);
<#
break;
case "long":
#>amd
= new
IntegerAttributeMetadata();
amd.SchemaName = _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
amd.DisplayName = new
Microsoft.Xrm.Sdk.Label("<#= fieldName #>", 1033);
amd.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, amd);
<#
break;
case "double":
#>amd
= new
DoubleAttributeMetadata();
amd.SchemaName = _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
amd.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
amd.DisplayName = new
Microsoft.Xrm.Sdk.Label("<#= fieldName #>", 1033);
amd.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, amd);
<#
break;
case "string":
int maxLength = 0;
Int32.TryParse(edmProperty.TypeUsage.Facets["MaxLength"].Value.ToString(), out maxLength); #>
<#
if(maxLength==0){ #>
mam
= new
MemoAttributeMetadata();
mam.MaxLength
= 1048576;
mam.SchemaName
= _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
mam.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
mam.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
mam.DisplayName = new Microsoft.Xrm.Sdk.Label("<#= fieldName
#>", 1033);
mam.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, mam);
<#
} else{ #>
sam = new StringAttributeMetadata();
sam.MaxLength
= <#= maxLength #>;
sam.SchemaName
= _prefix + "<#= fieldName #>";
<#
if(isNullable){ #>
sam.RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
<#
} else { #>
sam.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
<#
} #>
sam.DisplayName
= new
Microsoft.Xrm.Sdk.Label("<#= fieldName #>", 1033);
sam.Description
= new
Microsoft.Xrm.Sdk.Label("Generated with a tool", 1033);
CreateAttribute(_service,
_prefix + entityName, sam);
<#
} #>
<#
break;
case "TimeSpan":
#>
//TimeSpan attribute
not supported for field <#= fieldName #>
<#
break;
default:
#><#
break;
}
#>
<#}#>
<#}#>
<#}#>
}
}
}