mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-05-16 21:19:37 +08:00
294 lines
7.9 KiB
Plaintext
294 lines
7.9 KiB
Plaintext
<%@ Template Language="C#" TargetLanguage="C#" Debug="True" Encoding="UTF-8" %>
|
|
|
|
<%@ Assembly Src="Model.cs" %>
|
|
<%@ Assembly Src="Extensions.cs" %>
|
|
|
|
<%@ Import Namespace="System.Collections.Generic" %>
|
|
<%@ Import Namespace="System.Linq" %>
|
|
<%@ Import Namespace="System.Text" %>
|
|
<%@ Import Namespace="System.Text.RegularExpressions" %>
|
|
|
|
<%@ Import Namespace="SchemaMapper" %>
|
|
|
|
<%@ Property Name="Entity" Type="SchemaMapper.Entity" %>
|
|
<%@ Property Name="ContextNamespace" Type="System.String" %>
|
|
<%@ Property Name="EntityNamespace" Type="System.String" %>
|
|
<%@ Property Name="MappingNamespace" Type="System.String" %>
|
|
//------------------------------------------------------------------------------
|
|
// <autogenerated>
|
|
// This code was generated by a CodeSmith Template.
|
|
//
|
|
// DO NOT MODIFY contents of this file. Changes to this
|
|
// file will be lost if the code is regenerated.
|
|
// </autogenerated>
|
|
//------------------------------------------------------------------------------
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
namespace <%= MappingNamespace %>
|
|
{
|
|
public partial class <%= Entity.MappingName.ToSafeName() %>
|
|
: System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<<%= EntityNamespace %>.<%= Entity.ClassName.ToSafeName() %>>
|
|
{
|
|
public <%= Entity.MappingName.ToSafeName() %>()
|
|
{
|
|
// table
|
|
<% if (string.IsNullOrEmpty(Entity.TableSchema)) { %>
|
|
ToTable("<%= Entity.TableName %>");
|
|
<% } else { %>
|
|
ToTable("<%= Entity.TableName %>", "<%= Entity.TableSchema %>");
|
|
<% } %>
|
|
|
|
// keys
|
|
<% if (Entity.Properties.PrimaryKeys.Count() > 0) { %>
|
|
HasKey(t => <%= KeyExpression(Entity.Properties.PrimaryKeys, "t") %>);
|
|
<% } %>
|
|
|
|
// Properties
|
|
<%
|
|
foreach(var p in Entity.Properties)
|
|
{
|
|
Response.Write(PropertyExpression(p));
|
|
}
|
|
%>
|
|
|
|
// Relationships
|
|
<%
|
|
foreach(var r in Entity.Relationships.Where(e => e.IsMapped))
|
|
{
|
|
if (r.IsManyToMany)
|
|
Response.Write(ManyToManyExpression(r));
|
|
else
|
|
Response.Write(RelationshipExpression(r));
|
|
}
|
|
%>
|
|
}
|
|
}
|
|
}
|
|
|
|
<script runat="template">
|
|
|
|
public string PropertyExpression(Property property)
|
|
{
|
|
bool isString = property.SystemType == typeof(string);
|
|
bool isByteArray = property.SystemType == typeof(byte[]);
|
|
bool isDecimal = property.SystemType == typeof(Decimal);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append(' ', 3 * 4);
|
|
sb.Append("Property(t => t.");
|
|
sb.Append(property.PropertyName);
|
|
sb.Append(")");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".HasColumnName(\"");
|
|
sb.Append(property.ColumnName);
|
|
sb.Append("\")");
|
|
|
|
if (property.IsIdentity == true)
|
|
{
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)");
|
|
}
|
|
else if (property.IsAutoGenerated == true)
|
|
{
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)");
|
|
}
|
|
|
|
if ((isString || isByteArray) && (property.MaxLength > 0 && property.MaxLength < 8000))
|
|
{
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".HasMaxLength(");
|
|
sb.Append(property.MaxLength);
|
|
sb.Append(")");
|
|
}
|
|
if (isDecimal && property.Precision.HasValue && property.Scale.HasValue)
|
|
{
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".HasPrecision(");
|
|
sb.Append(property.Precision);
|
|
sb.Append(", ");
|
|
sb.Append(property.Scale);
|
|
sb.Append(")");
|
|
}
|
|
if (property.IsRowVersion == true && isByteArray)
|
|
{
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".IsRowVersion()");
|
|
}
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
if (property.IsRequired == true)
|
|
sb.Append(".IsRequired()");
|
|
else
|
|
sb.Append(".IsOptional()");
|
|
|
|
sb.AppendLine(";");
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public string ManyToManyExpression(Relationship relationship)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append(' ', 3 * 4);
|
|
sb.Append("HasMany(t => t.");
|
|
sb.Append(relationship.ThisPropertyName);
|
|
sb.Append(")");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".WithMany(t => t.");
|
|
sb.Append(relationship.OtherPropertyName);
|
|
sb.Append(")");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".Map(m =>");
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append("{");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 5 * 4);
|
|
sb.Append("m.ToTable(\"");
|
|
sb.Append(relationship.JoinTable);
|
|
sb.Append("\", \"");
|
|
sb.Append(relationship.JoinSchema);
|
|
sb.Append("\");");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 5 * 4);
|
|
sb.Append("m.MapLeftKey(");
|
|
sb.Append(relationship.JoinThisColumn.ToDelimitedString(", ", "\"{0}\""));
|
|
sb.Append(");");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 5 * 4);
|
|
sb.Append("m.MapRightKey(");
|
|
sb.Append(relationship.JoinOtherColumn.ToDelimitedString(", ", "\"{0}\""));
|
|
sb.Append(");");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append("})");
|
|
|
|
sb.Append(";");
|
|
sb.AppendLine();
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public string RelationshipExpression(Relationship relationship)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append(' ', 3 * 4);
|
|
|
|
if (relationship.ThisCardinality == Cardinality.One)
|
|
{
|
|
sb.Append("HasRequired(t => t.");
|
|
sb.Append(relationship.ThisPropertyName);
|
|
sb.Append(")");
|
|
}
|
|
else if (relationship.ThisCardinality == Cardinality.ZeroOrOne)
|
|
{
|
|
sb.Append("HasOptional(t => t.");
|
|
sb.Append(relationship.ThisPropertyName);
|
|
sb.Append(")");
|
|
}
|
|
|
|
if (relationship.OtherCardinality == Cardinality.Many)
|
|
{
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".WithMany(t => t.");
|
|
sb.Append(relationship.OtherPropertyName);
|
|
sb.Append(")");
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".HasForeignKey(d => ");
|
|
sb.Append(KeyExpression(relationship.ThisProperties, "d"));
|
|
sb.Append(")");
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".WithOptional(t => t.");
|
|
sb.Append(relationship.OtherPropertyName);
|
|
sb.Append(")");
|
|
}
|
|
|
|
sb.AppendLine();
|
|
sb.Append(' ', 4 * 4);
|
|
sb.Append(".WillCascadeOnDelete(");
|
|
sb.Append(relationship.CascadeDelete == true ? "true" : "false");
|
|
sb.Append(")");
|
|
|
|
sb.AppendLine(";");
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public string KeyExpression(IEnumerable<Property> keys, string alias = "t")
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
if (keys.Count() == 1)
|
|
{
|
|
sb.Append(alias);
|
|
sb.Append(".");
|
|
sb.Append(keys.FirstOrDefault().PropertyName);
|
|
}
|
|
else
|
|
{
|
|
sb.Append("new { ");
|
|
foreach(var p in keys)
|
|
{
|
|
if (sb.Length > 6)
|
|
sb.Append(", ");
|
|
|
|
sb.Append(alias);
|
|
sb.Append(".");
|
|
sb.Append(p.PropertyName);
|
|
}
|
|
sb.Append(" }");
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
public string KeyExpression(IEnumerable<string> keys, string alias = "t")
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
if (keys.Count() == 1)
|
|
{
|
|
sb.Append(alias);
|
|
sb.Append(".");
|
|
sb.Append(keys.FirstOrDefault());
|
|
}
|
|
else
|
|
{
|
|
sb.Append("new { ");
|
|
foreach(var p in keys)
|
|
{
|
|
if (sb.Length > 6)
|
|
sb.Append(", ");
|
|
|
|
sb.Append(alias);
|
|
sb.Append(".");
|
|
sb.Append(p);
|
|
}
|
|
sb.Append(" }");
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
</script> |