根据Entity Framework6.X 数据生成代码(仅获取表名)

近来学习ASP.NET MVC ,一直在看韩迎龙(Kencery)ASP.NET MVC+EF框架+EasyUI实现权限管理。在第九节(TT模板的学习)卡住了,作者使用EF5.0做数据源,而我使用的EF6.0,在代码生成时引用的ttinclude包不一样。EF5使用的是EF.Utility.CS.ttinclude,而EF6使用的是EF6.Utility.CS.ttinclude,两者部分代码完全不一样,使用方法也有很大区别,博客园,度娘,谷歌,都找了,找不到一篇关于EF6.Utility.CS.ttinclude这个文件的使用或内容介绍的文章,无奈只得自己摸索,无意间发现.edmx文件的添加代码生成项生成的文件接近我要生成的代码,所以借用此代码并修改成我想要的代码.不太理解,仅代码贴上,以供以后使用.(本篇获取表名,并将所有表名生成在一个文件中)。

这是设置部分(里面的const string inputFile = @"..\\DataModel\\DataModel.edmx"; 部分需要改成自己的数据源.

Code Snippet
  1. <#@ template language="C#" debug="false" hostspecific="true"#>
  2. <#@ include file="EF6.Utility.CS.ttinclude"#><#@ output extension=".cs"#>
  3.   <#const string inputFile = @"..\\DataModel\\DataModel.edmx";
  4.     var textTransform = DynamicTextTransformation.Create(this);
  5.     var code = new CodeGenerationTools(this);
  6.     var ef = new MetadataTools(this);
  7.     var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
  8.     var loader = new EdmMetadataLoader(textTransform.Host, textTransform.Errors);
  9.     var itemCollection = loader.CreateEdmItemCollection(inputFile);
  10.     var modelNamespace = loader.GetModelNamespace(inputFile);
  11.     var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
  12.     var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
  13.     if (container == null)
  14.     {
  15.         return string.Empty;
  16.     }#>

 

这是要生成的部分,建议先写好一个完整代码,然后在内部替换.一定要注意里面的各种符号的配对!!!!

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. //new using
  8. using IDAL;
  9. using System.Data.Common;
  10.  
  11. <#
  12.  
  13. var codeNamespace = code.VsNamespaceSuggestion();
  14. if (!String.IsNullOrEmpty(codeNamespace))
  15. {
  16. #>
  17. namespace <#=code.EscapeNamespace(codeNamespace)#>
  18. {
  19. <#
  20.     PushIndent("    ");
  21. }
  22.  
  23. #>
  24. public partial interface IDBSession
  25. {
  26.     //
  27. <#
  28.     foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
  29.     {
  30. #>
  31.  
  32.     /// <summary>
  33.     /// <#=entitySet #>
  34.     /// </summary>
  35.     <#="I"+entitySet+"Repository "+entitySet+"Repository { get; }" #>
  36. <#
  37.     }
  38. #>
  39.  
  40.  
  41.     //
  42.     int SaveChanges( );
  43.  
  44.     int ExcuteSql( string strSql, DbParameter[] parameters );
  45. }
  46. <#
  47.  
  48. if (!String.IsNullOrEmpty(codeNamespace))
  49. {
  50.     PopIndent();
  51. #>
  52. }
  53. <#
  54. }
  55. #>

 

下面这一部分完全是不需要改变的,这是生成代码使用的方法,属性,字段等,直接贴上来.

Code Snippet
  1. <#+
  2. private void WriteFunctionImport(TypeMapper typeMapper, CodeStringGenerator codeStringGenerator, EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
  3. {
  4.     if (typeMapper.IsComposable(edmFunction))
  5.     {
  6. #>
  7.  
  8.     [DbFunction("<#=edmFunction.NamespaceName#>", "<#=edmFunction.Name#>")]
  9.     <#=codeStringGenerator.ComposableFunctionMethod(edmFunction, modelNamespace)#>
  10.     {
  11. <#+
  12.         codeStringGenerator.WriteFunctionParameters(edmFunction, WriteFunctionParameter);
  13. #>
  14.         <#=codeStringGenerator.ComposableCreateQuery(edmFunction, modelNamespace)#>
  15.     }
  16. <#+
  17.     }
  18.     else
  19.     {
  20. #>
  21.  
  22.     <#=codeStringGenerator.FunctionMethod(edmFunction, modelNamespace, includeMergeOption)#>
  23.     {
  24. <#+
  25.         codeStringGenerator.WriteFunctionParameters(edmFunction, WriteFunctionParameter);
  26. #>
  27.         <#=codeStringGenerator.ExecuteFunction(edmFunction, modelNamespace, includeMergeOption)#>
  28.     }
  29. <#+
  30.         if (typeMapper.GenerateMergeOptionFunction(edmFunction, includeMergeOption))
  31.         {
  32.             WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption: true);
  33.         }
  34.     }
  35. }
  36.  
  37. public void WriteFunctionParameter(string name, string isNotNull, string notNullInit, string nullInit)
  38. {
  39. #>
  40.         var <#=name#> = <#=isNotNull#> ?
  41.             <#=notNullInit#> :
  42.             <#=nullInit#>;
  43.  
  44. <#+
  45. }
  46.  
  47. public const string TemplateId = "CSharp_DbContext_Context_EF6";
  48.  
  49. public class CodeStringGenerator
  50. {
  51.     private readonly CodeGenerationTools _code;
  52.     private readonly TypeMapper _typeMapper;
  53.     private readonly MetadataTools _ef;
  54.  
  55.     public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef)
  56.     {
  57.         ArgumentNotNull(code, "code");
  58.         ArgumentNotNull(typeMapper, "typeMapper");
  59.         ArgumentNotNull(ef, "ef");
  60.  
  61.         _code = code;
  62.         _typeMapper = typeMapper;
  63.         _ef = ef;
  64.     }
  65.  
  66.     public string Property(EdmProperty edmProperty)
  67.     {
  68.         return string.Format(
  69.             CultureInfo.InvariantCulture,
  70.             "{0} {1} {2} {{ {3}get; {4}set; }}",
  71.             Accessibility.ForProperty(edmProperty),
  72.             _typeMapper.GetTypeName(edmProperty.TypeUsage),
  73.             _code.Escape(edmProperty),
  74.             _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
  75.             _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  76.     }
  77.  
  78.     public string NavigationProperty(NavigationProperty navProp)
  79.     {
  80.         var endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType());
  81.         return string.Format(
  82.             CultureInfo.InvariantCulture,
  83.             "{0} {1} {2} {{ {3}get; {4}set; }}",
  84.             AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)),
  85.             navProp.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
  86.             _code.Escape(navProp),
  87.             _code.SpaceAfter(Accessibility.ForGetter(navProp)),
  88.             _code.SpaceAfter(Accessibility.ForSetter(navProp)));
  89.     }
  90.     
  91.     public string AccessibilityAndVirtual(string accessibility)
  92.     {
  93.         return accessibility + (accessibility != "private" ? " virtual" : "");
  94.     }
  95.     
  96.     public string EntityClassOpening(EntityType entity)
  97.     {
  98.         return string.Format(
  99.             CultureInfo.InvariantCulture,
  100.             "{0} {1}partial class {2}{3}",
  101.             Accessibility.ForType(entity),
  102.             _code.SpaceAfter(_code.AbstractOption(entity)),
  103.             _code.Escape(entity),
  104.             _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
  105.     }
  106.     
  107.     public string EnumOpening(SimpleType enumType)
  108.     {
  109.         return string.Format(
  110.             CultureInfo.InvariantCulture,
  111.             "{0} enum {1} : {2}",
  112.             Accessibility.ForType(enumType),
  113.             _code.Escape(enumType),
  114.             _code.Escape(_typeMapper.UnderlyingClrType(enumType)));
  115.         }
  116.     
  117.     public void WriteFunctionParameters(EdmFunction edmFunction, Action<string, string, string, string> writeParameter)
  118.     {
  119.         var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
  120.         foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
  121.         {
  122.             var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
  123.             var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
  124.             var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + TypeMapper.FixNamespaces(parameter.RawClrTypeName) + "))";
  125.             writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit);
  126.         }
  127.     }
  128.     
  129.     public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace)
  130.     {
  131.         var parameters = _typeMapper.GetParameters(edmFunction);
  132.         
  133.         return string.Format(
  134.             CultureInfo.InvariantCulture,
  135.             "{0} IQueryable<{1}> {2}({3})",
  136.             AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
  137.             _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
  138.             _code.Escape(edmFunction),
  139.             string.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray()));
  140.     }
  141.     
  142.     public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace)
  143.     {
  144.         var parameters = _typeMapper.GetParameters(edmFunction);
  145.         
  146.         return string.Format(
  147.             CultureInfo.InvariantCulture,
  148.             "return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<{0}>(\"[{1}].[{2}]({3})\"{4});",
  149.             _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
  150.             edmFunction.NamespaceName,
  151.             edmFunction.Name,
  152.             string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()),
  153.             _code.StringBefore(", ", string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())));
  154.     }
  155.     
  156.     public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
  157.     {
  158.         var parameters = _typeMapper.GetParameters(edmFunction);
  159.         var returnType = _typeMapper.GetReturnType(edmFunction);
  160.  
  161.         var paramList = String.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray());
  162.         if (includeMergeOption)
  163.         {
  164.             paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
  165.         }
  166.  
  167.         return string.Format(
  168.             CultureInfo.InvariantCulture,
  169.             "{0} {1} {2}({3})",
  170.             AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
  171.             returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
  172.             _code.Escape(edmFunction),
  173.             paramList);
  174.     }
  175.     
  176.     public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
  177.     {
  178.         var parameters = _typeMapper.GetParameters(edmFunction);
  179.         var returnType = _typeMapper.GetReturnType(edmFunction);
  180.  
  181.         var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
  182.         if (includeMergeOption)
  183.         {
  184.             callParams = ", mergeOption" + callParams;
  185.         }
  186.         
  187.         return string.Format(
  188.             CultureInfo.InvariantCulture,
  189.             "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});",
  190.             returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
  191.             edmFunction.Name,
  192.             callParams);
  193.     }
  194.     
  195.     public string DbSet(EntitySet entitySet)
  196.     {
  197.         return string.Format(
  198.             CultureInfo.InvariantCulture,
  199.             "{0} virtual DbSet<{1}> {2} {{ get; set; }}",
  200.             Accessibility.ForReadOnlyProperty(entitySet),
  201.             _typeMapper.GetTypeName(entitySet.ElementType),
  202.             _code.Escape(entitySet));
  203.     }
  204.  
  205.     public string DbSetInitializer(EntitySet entitySet)
  206.     {
  207.         return string.Format(
  208.             CultureInfo.InvariantCulture,
  209.             "{0} = Set<{1}>();",
  210.             _code.Escape(entitySet),
  211.             _typeMapper.GetTypeName(entitySet.ElementType));
  212.     }
  213.  
  214.     public string UsingDirectives(bool inHeader, bool includeCollections = true)
  215.     {
  216.         return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
  217.             ? string.Format(
  218.                 CultureInfo.InvariantCulture,
  219.                 "{0}using System;{1}" +
  220.                 "{2}",
  221.                 inHeader ? Environment.NewLine : "",
  222.                 includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
  223.                 inHeader ? "" : Environment.NewLine)
  224.             : "";
  225.     }
  226. }
  227.  
  228. public class TypeMapper
  229. {
  230.     private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName";
  231.  
  232.     private readonly System.Collections.IList _errors;
  233.     private readonly CodeGenerationTools _code;
  234.     private readonly MetadataTools _ef;
  235.  
  236.     public static string FixNamespaces(string typeName)
  237.     {
  238.         return typeName.Replace("System.Data.Spatial.", "System.Data.Entity.Spatial.");
  239.     }
  240.  
  241.     public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors)
  242.     {
  243.         ArgumentNotNull(code, "code");
  244.         ArgumentNotNull(ef, "ef");
  245.         ArgumentNotNull(errors, "errors");
  246.  
  247.         _code = code;
  248.         _ef = ef;
  249.         _errors = errors;
  250.     }
  251.  
  252.     public string GetTypeName(TypeUsage typeUsage)
  253.     {
  254.         return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null);
  255.     }
  256.  
  257.     public string GetTypeName(EdmType edmType)
  258.     {
  259.         return GetTypeName(edmType, isNullable: null, modelNamespace: null);
  260.     }
  261.  
  262.     public string GetTypeName(TypeUsage typeUsage, string modelNamespace)
  263.     {
  264.         return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace);
  265.     }
  266.  
  267.     public string GetTypeName(EdmType edmType, string modelNamespace)
  268.     {
  269.         return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace);
  270.     }
  271.  
  272.     public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace)
  273.     {
  274.         if (edmType == null)
  275.         {
  276.             return null;
  277.         }
  278.  
  279.         var collectionType = edmType as CollectionType;
  280.         if (collectionType != null)
  281.         {
  282.             return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace));
  283.         }
  284.  
  285.         var typeName = _code.Escape(edmType.MetadataProperties
  286.                                 .Where(p => p.Name == ExternalTypeNameAttributeName)
  287.                                 .Select(p => (string)p.Value)
  288.                                 .FirstOrDefault())
  289.             ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ?
  290.                 _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) :
  291.                 _code.Escape(edmType));
  292.  
  293.         if (edmType is StructuralType)
  294.         {
  295.             return typeName;
  296.         }
  297.  
  298.         if (edmType is SimpleType)
  299.         {
  300.             var clrType = UnderlyingClrType(edmType);
  301.             if (!IsEnumType(edmType))
  302.             {
  303.                 typeName = _code.Escape(clrType);
  304.             }
  305.  
  306.             typeName = FixNamespaces(typeName);
  307.  
  308.             return clrType.IsValueType && isNullable == true ?
  309.                 String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) :
  310.                 typeName;
  311.         }
  312.  
  313.         throw new ArgumentException("edmType");
  314.     }
  315.     
  316.     public Type UnderlyingClrType(EdmType edmType)
  317.     {
  318.         ArgumentNotNull(edmType, "edmType");
  319.  
  320.         var primitiveType = edmType as PrimitiveType;
  321.         if (primitiveType != null)
  322.         {
  323.             return primitiveType.ClrEquivalentType;
  324.         }
  325.  
  326.         if (IsEnumType(edmType))
  327.         {
  328.             return GetEnumUnderlyingType(edmType).ClrEquivalentType;
  329.         }
  330.  
  331.         return typeof(object);
  332.     }
  333.     
  334.     public object GetEnumMemberValue(MetadataItem enumMember)
  335.     {
  336.         ArgumentNotNull(enumMember, "enumMember");
  337.         
  338.         var valueProperty = enumMember.GetType().GetProperty("Value");
  339.         return valueProperty == null ? null : valueProperty.GetValue(enumMember, null);
  340.     }
  341.     
  342.     public string GetEnumMemberName(MetadataItem enumMember)
  343.     {
  344.         ArgumentNotNull(enumMember, "enumMember");
  345.         
  346.         var nameProperty = enumMember.GetType().GetProperty("Name");
  347.         return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null);
  348.     }
  349.  
  350.     public System.Collections.IEnumerable GetEnumMembers(EdmType enumType)
  351.     {
  352.         ArgumentNotNull(enumType, "enumType");
  353.  
  354.         var membersProperty = enumType.GetType().GetProperty("Members");
  355.         return membersProperty != null
  356.             ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null)
  357.             : Enumerable.Empty<MetadataItem>();
  358.     }
  359.     
  360.     public bool EnumIsFlags(EdmType enumType)
  361.     {
  362.         ArgumentNotNull(enumType, "enumType");
  363.         
  364.         var isFlagsProperty = enumType.GetType().GetProperty("IsFlags");
  365.         return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null);
  366.     }
  367.  
  368.     public bool IsEnumType(GlobalItem edmType)
  369.     {
  370.         ArgumentNotNull(edmType, "edmType");
  371.  
  372.         return edmType.GetType().Name == "EnumType";
  373.     }
  374.  
  375.     public PrimitiveType GetEnumUnderlyingType(EdmType enumType)
  376.     {
  377.         ArgumentNotNull(enumType, "enumType");
  378.  
  379.         return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null);
  380.     }
  381.  
  382.     public string CreateLiteral(object value)
  383.     {
  384.         if (value == null || value.GetType() != typeof(TimeSpan))
  385.         {
  386.             return _code.CreateLiteral(value);
  387.         }
  388.  
  389.         return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks);
  390.     }
  391.     
  392.     public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable<string> types, string sourceFile)
  393.     {
  394.         ArgumentNotNull(types, "types");
  395.         ArgumentNotNull(sourceFile, "sourceFile");
  396.         
  397.         var hash = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  398.         if (types.Any(item => !hash.Add(item)))
  399.         {
  400.             _errors.Add(
  401.                 new CompilerError(sourceFile, -1, -1, "6023",
  402.                     String.Format(CultureInfo.CurrentCulture, CodeGenerationTools.GetResourceString("Template_CaseInsensitiveTypeConflict"))));
  403.             return false;
  404.         }
  405.         return true;
  406.     }
  407.     
  408.     public IEnumerable<SimpleType> GetEnumItemsToGenerate(IEnumerable<GlobalItem> itemCollection)
  409.     {
  410.         return GetItemsToGenerate<SimpleType>(itemCollection)
  411.             .Where(e => IsEnumType(e));
  412.     }
  413.     
  414.     public IEnumerable<T> GetItemsToGenerate<T>(IEnumerable<GlobalItem> itemCollection) where T: EdmType
  415.     {
  416.         return itemCollection
  417.             .OfType<T>()
  418.             .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName))
  419.             .OrderBy(i => i.Name);
  420.     }
  421.  
  422.     public IEnumerable<string> GetAllGlobalItems(IEnumerable<GlobalItem> itemCollection)
  423.     {
  424.         return itemCollection
  425.             .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i))
  426.             .Select(g => GetGlobalItemName(g));
  427.     }
  428.  
  429.     public string GetGlobalItemName(GlobalItem item)
  430.     {
  431.         if (item is EdmType)
  432.         {
  433.             return ((EdmType)item).Name;
  434.         }
  435.         else
  436.         {
  437.             return ((EntityContainer)item).Name;
  438.         }
  439.     }
  440.  
  441.     public IEnumerable<EdmProperty> GetSimpleProperties(EntityType type)
  442.     {
  443.         return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
  444.     }
  445.     
  446.     public IEnumerable<EdmProperty> GetSimpleProperties(ComplexType type)
  447.     {
  448.         return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
  449.     }
  450.     
  451.     public IEnumerable<EdmProperty> GetComplexProperties(EntityType type)
  452.     {
  453.         return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
  454.     }
  455.     
  456.     public IEnumerable<EdmProperty> GetComplexProperties(ComplexType type)
  457.     {
  458.         return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
  459.     }
  460.  
  461.     public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(EntityType type)
  462.     {
  463.         return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
  464.     }
  465.     
  466.     public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(ComplexType type)
  467.     {
  468.         return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
  469.     }
  470.  
  471.     public IEnumerable<NavigationProperty> GetNavigationProperties(EntityType type)
  472.     {
  473.         return type.NavigationProperties.Where(np => np.DeclaringType == type);
  474.     }
  475.     
  476.     public IEnumerable<NavigationProperty> GetCollectionNavigationProperties(EntityType type)
  477.     {
  478.         return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
  479.     }
  480.     
  481.     public FunctionParameter GetReturnParameter(EdmFunction edmFunction)
  482.     {
  483.         ArgumentNotNull(edmFunction, "edmFunction");
  484.  
  485.         var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters");
  486.         return returnParamsProperty == null
  487.             ? edmFunction.ReturnParameter
  488.             : ((IEnumerable<FunctionParameter>)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault();
  489.     }
  490.  
  491.     public bool IsComposable(EdmFunction edmFunction)
  492.     {
  493.         ArgumentNotNull(edmFunction, "edmFunction");
  494.  
  495.         var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute");
  496.         return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null);
  497.     }
  498.  
  499.     public IEnumerable<FunctionImportParameter> GetParameters(EdmFunction edmFunction)
  500.     {
  501.         return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
  502.     }
  503.  
  504.     public TypeUsage GetReturnType(EdmFunction edmFunction)
  505.     {
  506.         var returnParam = GetReturnParameter(edmFunction);
  507.         return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage);
  508.     }
  509.     
  510.     public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption)
  511.     {
  512.         var returnType = GetReturnType(edmFunction);
  513.         return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType;
  514.     }
  515. }
  516.  
  517. public static void ArgumentNotNull<T>(T arg, string name) where T : class
  518. {
  519.     if (arg == null)
  520.     {
  521.         throw new ArgumentNullException(name);
  522.     }
  523. }
  524. #>

你可能感兴趣的:(framework)