1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<#
// ++++++++++++++++++++++++++++++++++++++++++++++++++
// NOTE: This include template also requires an assembly
// reference to $(DevEnvDir)\Microsoft.Data.Entity.Design.DatabaseGeneration.dll
// and a namespace reference to Microsoft.Data.Entity.Design.DatabaseGeneration
// in the parent template. Commented-out examples are provided below (replace '!' with '#'):
// <!@ assembly name="$(DevEnvDir)Microsoft.Data.Entity.Design.DatabaseGeneration.dll" !>
// <!@ import namespace="Microsoft.Data.Entity.Design.DatabaseGeneration" !>
// ++++++++++++++++++++++++++++++++++++++++++++++++++
#>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Data.Entity.Design" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.Entity.Design" #>
<#@ import namespace="System.Data.Metadata.Edm" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#+
// ++++++++++++++++++++++++++++++++++++++++++++++++++
// Common Helper properties and methods for the T4 templates
// used by the 'Generate Database' feature
// in the Entity Designer.
// ++++++++++++++++++++++++++++++++++++++++++++++++++
private static readonly string PROVIDER_NAME_MYSQL = "MySql.Data.MySqlClient";
/// <summary>
/// Determine if the given provider invariant name is MySQL
/// </summary>
private bool IsMySql
{
get
{
return (this.ProviderInvariantName.Equals(PROVIDER_NAME_MYSQL, StringComparison.Ordinal));
}
}
/// <summary>
/// Escape identifiers enclosed by backticks within the template
/// </summary>
private static string MyId(string userIdentifier)
{
if (String.IsNullOrEmpty(userIdentifier)) return userIdentifier;
return userIdentifier.Replace("`", "``");
}
private static bool IsIdentity(EdmProperty property, Version targetVersion)
{
return (property.GetStoreGeneratedPatternValue(targetVersion, DataSpace.SSpace) == StoreGeneratedPattern.Identity
&& true == IsIntegerOrDecimalType(property));
}
private static string WriteMySqlType(EdmProperty property)
{
if (property.TypeUsage.EdmType.Name == "guid")
return "CHAR(36) BINARY";
return property.ToStoreType();
}
/// <summary>
/// If the property has a StoreGeneratedPattern=Identity annotation and
/// is of an integer or decimal type, write 'IDENTITY(1,1)', otherwise an empty string
/// </summary>
private static string WriteMySqlIdentity(EdmProperty property, Version targetVersion)
{
if (IsIdentity(property, targetVersion))
{
return "AUTO_INCREMENT PRIMARY KEY";
}
return String.Empty;
}
/// <summary>
/// Write out the columns of a table given the Properties of an EntityType,
/// escaping the identifier names.
/// </summary>
private static string WriteMySqlColumns(IEnumerable<EdmProperty> properties, char delimiter)
{
StringBuilder serializedProperties = new StringBuilder();
foreach (EdmProperty property in properties)
{
serializedProperties.Append(string.Format("`{0}`", MyId(property.Name)));
serializedProperties.Append(delimiter + " ");
}
return serializedProperties.ToString().Trim().TrimEnd(delimiter);
}
#>
|