-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDbMetadataExtensions.cs
More file actions
82 lines (74 loc) · 3.22 KB
/
Copy pathDbMetadataExtensions.cs
File metadata and controls
82 lines (74 loc) · 3.22 KB
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
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Linq;
namespace UdfCodeFirstSample
{
public static class DbMetadataExtensions
{
public static EdmFunction CreateAndAddFunction(this EdmModel item, string name,
IList<FunctionParameter> parameters, IList<FunctionParameter> returnValues, string body = null)
{
var payload = new EdmFunctionPayload
{
StoreFunctionName = name,
Parameters = parameters,
ReturnParameters = returnValues,
Schema = item.GetDefaultSchema()
};
EdmFunction function = EdmFunction.Create(name, item.GetDefaultNamespace(), item.DataSpace, payload, null);
item.AddItem(function);
return function;
}
/// <summary>
/// Translate a conceptual primitive type to an adequate store specific primitive type according to the
/// provider configuration of the model.
/// </summary>
/// <param name="model">A DbModel instance with provider information</param>
/// <param name="typeKind">A PrimitiveTypeKind instance representing the conceptual primitive type to be translated</param>
/// <returns>An EdmType instance representing the store primitive type</returns>
public static EdmType GetStorePrimitiveType(this DbModel model, PrimitiveTypeKind typeKind)
{
return model
.ProviderManifest
.GetStoreType(
TypeUsage.CreateDefaultTypeUsage(
PrimitiveType.GetEdmPrimitiveType(typeKind))).EdmType;
}
/// <summary>
/// Obtain the namespace name from existing model defined types.
/// </summary>
/// <param name="layerModel">An EdmModel instance representing conceptual or store model.</param>
/// <returns>A string contining the name of the namespace.</returns>
/// <remarks>
/// Only one namespace is allowed. Throws if there are multiple namespaces or if there aren't any types defined in the
/// model.
/// </remarks>
public static string GetDefaultNamespace(this EdmModel layerModel)
{
return layerModel
.GlobalItems
.OfType<EdmType>()
.Select(t => t.NamespaceName)
.Distinct()
.Single();
}
/// <summary>
/// Obtains the default schema from existing entity sets in the model.
/// </summary>
/// <param name="layerModel">An instance of EdmModel representing either the conceptual or the store model.</param>
/// <returns>A string containing the name of the schema.</returns>
/// <remarks>
/// Throws if more than one schema is used or if the model contains no entity sets.
/// </remarks>
public static string GetDefaultSchema(this EdmModel layerModel)
{
return layerModel
.Container
.EntitySets
.Select(s => s.Schema)
.Distinct()
.SingleOrDefault();
}
}
}