123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <#@ template language="C#" debug="false" hostspecific="true"#>
- <#@ include file="EF.Utility.CS.ttinclude"#><#@
- output extension=".cs"#><#
- var loader = new MetadataLoader(this);
- var region = new CodeRegion(this);
- var inputFile = @"EMISContext.edmx";
- var ItemCollection = loader.CreateEdmItemCollection(inputFile);
- Code = new CodeGenerationTools(this);
- EFTools = new MetadataTools(this);
- ObjectNamespace = Code.VsNamespaceSuggestion();
- ModelNamespace = loader.GetModelNamespace(inputFile);
- EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();
- if (container == null)
- {
- return string.Empty;
- }
- #>
- //------------------------------------------------------------------------------
- // <auto-generated>
- // This code was generated from a template.
- //
- // Manual changes to this file may cause unexpected behavior in your application.
- // Manual changes to this file will be overwritten if the code is regenerated.
- // </auto-generated>
- //------------------------------------------------------------------------------
- #pragma warning disable 1573
- <#
- if (!String.IsNullOrEmpty(ObjectNamespace))
- {
- #>
- namespace <#=Code.EscapeNamespace(ObjectNamespace)#>
- {
- <#
- PushIndent(CodeRegion.GetIndent(1));
- }
- #>
- using System;
- using System.Data.Common;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- <#
- if (container.FunctionImports.Any())
- {
- #>
- using System.Data.Objects;
- <#
- }
- #>
- <#=Accessibility.ForType(container)#> partial class <#=Code.Escape(container)#> : DbContext
- {
- static <#=Code.Escape(container)#>()
- {
- Database.SetInitializer<<#=Code.Escape(container)#>>(null);
- }
-
- public <#=Code.Escape(container)#>() : base("name=<#=container.Name#>") {
- var objectContext = (this as IObjectContextAdapter).ObjectContext;
- objectContext.CommandTimeout = 12000;
- base.Configuration.ProxyCreationEnabled = false;
- }
- public <#=Code.Escape(container)#>(string nameOrConnectionString) : base(nameOrConnectionString) {
- var objectContext = (this as IObjectContextAdapter).ObjectContext;
- objectContext.CommandTimeout = 12000;
- base.Configuration.ProxyCreationEnabled = false;
- }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
- <#
- foreach (EntityType entitySet in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
- {
- #>
- modelBuilder.Configurations.Add(new <#=Code.Escape(entitySet.Name)#>_Mapping());
- <#
- }
- #>
- }
-
- <#
- foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- <#=Accessibility.ForReadOnlyProperty(entitySet)#> DbSet<<#=Code.Escape(entitySet.ElementType)#>> <#=Code.Escape(entitySet)#> { get; set; }
- <#
- }
- foreach (var edmFunction in container.FunctionImports)
- {
- WriteFunctionImport(edmFunction, false);
- }
- #>
- }
- <#
- if (!String.IsNullOrEmpty(ObjectNamespace))
- {
- PopIndent();
- #>
- }
- <#
- }
- #>
- <#+
- string ModelNamespace { get; set; }
- string ObjectNamespace { get; set; }
- CodeGenerationTools Code { get; set; }
- MetadataTools EFTools { get; set; }
- void WriteLazyLoadingEnabled(EntityContainer container)
- {
- string lazyLoadingAttributeValue = null;
- var lazyLoadingAttributeName = MetadataConstants.EDM_ANNOTATION_09_02 + ":LazyLoadingEnabled";
- if(MetadataTools.TryGetStringMetadataPropertySetting(container, lazyLoadingAttributeName, out lazyLoadingAttributeValue))
- {
- bool isLazyLoading;
- if(bool.TryParse(lazyLoadingAttributeValue, out isLazyLoading) && !isLazyLoading)
- {
- #>
- this.Configuration.LazyLoadingEnabled = false;
- <#+
- }
- }
- }
- void WriteFunctionImport(EdmFunction edmFunction, bool includeMergeOption)
- {
- var parameters = FunctionImportParameter.Create(edmFunction.Parameters, Code, EFTools);
- var paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
- var returnType = edmFunction.ReturnParameter == null ? null : EFTools.GetElementType(edmFunction.ReturnParameter.TypeUsage);
- var processedReturn = returnType == null ? "int" : "ObjectResult<" + MultiSchemaEscape(returnType) + ">";
- if (includeMergeOption)
- {
- paramList = Code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
- }
- #>
- <#=AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction))#> <#=processedReturn#> <#=Code.Escape(edmFunction)#>(<#=paramList#>)
- {
- <#+
- if(returnType != null && (returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType ||
- returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType))
- {
- #>
- ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(<#=MultiSchemaEscape(returnType)#>).Assembly);
- <#+
- }
- foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
- {
- var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
- var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
- var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + parameter.RawClrTypeName + "))";
- #>
- var <#=parameter.LocalVariableName#> = <#=isNotNull#> ?
- <#=notNullInit#> :
- <#=nullInit#>;
- <#+
- }
- var genericArg = returnType == null ? "" : "<" + MultiSchemaEscape(returnType) + ">";
- var callParams = Code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
- if (includeMergeOption)
- {
- callParams = ", mergeOption" + callParams;
- }
- #>
- return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<#=genericArg#>("<#=edmFunction.Name#>"<#=callParams#>);
- }
- <#+
- if(!includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType)
- {
- WriteFunctionImport(edmFunction, true);
- }
- }
- string AccessibilityAndVirtual(string accessibility)
- {
- return accessibility + (accessibility != "private" ? " virtual" : "");
- }
- string MultiSchemaEscape(TypeUsage usage)
- {
- var type = usage.EdmType as StructuralType;
- return type != null && type.NamespaceName != ModelNamespace ?
- Code.CreateFullName(Code.EscapeNamespace(type.NamespaceName), Code.Escape(type)) :
- Code.Escape(usage);
- }
- #>
|