1 //---------------------------------------------------------------------
2 // <copyright file="MetadataItemCollectionFactory.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 namespace System.Data.Entity.Design
12 using System.Data.Entity;
13 using System.Data.EntityModel;
15 using System.Collections.Generic;
16 using System.Data.Common;
17 using System.Data.Metadata.Edm;
18 using System.Data.Mapping;
19 using System.Data.Entity.Design.Common;
20 using Microsoft.Build.Utilities;
21 using System.Data.Entity.Design.SsdlGenerator;
22 using System.Diagnostics;
26 /// Factory for creating ItemCollections. This class is to be used for
27 /// design time scenarios. The consumers of the methods in this class
28 /// will get an error list instead of an exception if there are errors in schema files.
31 public static class MetadataItemCollectionFactory
34 /// Create an EdmItemCollection with the passed in parameters.
35 /// Add any errors caused during the ItemCollection creation
36 /// to the error list passed in.
38 /// <param name="readers"></param>
39 /// <param name="errors"></param>
40 /// <returns></returns>
41 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Edm")]
42 public static EdmItemCollection CreateEdmItemCollection(IEnumerable<XmlReader> readers,
43 out IList<EdmSchemaError> errors)
45 System.Collections.ObjectModel.ReadOnlyCollection<string> filePaths = null;
46 return new EdmItemCollection(readers, filePaths, out errors);
50 /// Create an EdmItemCollection with the passed in parameters.
51 /// Add any errors caused during the ItemCollection creation
52 /// to the error list passed in.
54 /// <param name="readers"></param>
55 /// <param name="targetEntityFrameworkVersion"></param>
56 /// <param name="errors"></param>
57 /// <returns></returns>
58 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Edm")]
59 public static EdmItemCollection CreateEdmItemCollection(IEnumerable<XmlReader> readers,
60 Version targetEntityFrameworkVersion,
61 out IList<EdmSchemaError> errors)
63 EDesignUtil.CheckTargetEntityFrameworkVersionArgument(targetEntityFrameworkVersion, "targetEntityFrameworkVersion");
65 EdmItemCollection edmItemCollection = CreateEdmItemCollection(readers, out errors);
66 if (!errors.Any(e => e.Severity == EdmSchemaErrorSeverity.Error))
68 ValidateActualVersionAgainstTarget(targetEntityFrameworkVersion, EntityFrameworkVersionsUtil.ConvertToVersion(edmItemCollection.EdmVersion), errors);
71 return edmItemCollection;
74 internal static bool ValidateActualVersionAgainstTarget(Version maxExpectedVersion, Version actualVersion, IList<EdmSchemaError> errors)
76 if (!(actualVersion <= maxExpectedVersion))
78 errors.Add(new EdmSchemaError(Strings.TargetVersionSchemaVersionMismatch(maxExpectedVersion, actualVersion), (int)ModelBuilderErrorCode.SchemaVersionHigherThanTargetVersion, EdmSchemaErrorSeverity.Error));
85 /// Create an StoreItemCollection with the passed in parameters.
86 /// Add any errors caused during the ItemCollection creation
87 /// to the error list passed in.
89 /// <param name="connection"></param>
90 /// <param name="readers"></param>
91 /// <param name="errors"></param>
92 /// <returns></returns>
93 public static StoreItemCollection CreateStoreItemCollection(IEnumerable<XmlReader> readers,
94 out IList<EdmSchemaError> errors)
96 return new StoreItemCollection(readers, null, out errors);
100 /// Create an StoreItemCollection with the passed in parameters.
101 /// Add any errors caused during the ItemCollection creation
102 /// to the error list passed in.
104 /// <param name="connection"></param>
105 /// <param name="readers"></param>
106 /// <param name="targetEntityFrameworkVersion"></param>
107 /// <param name="errors"></param>
108 /// <returns></returns>
109 public static StoreItemCollection CreateStoreItemCollection(
110 IEnumerable<XmlReader> readers,
111 Version targetEntityFrameworkVersion,
112 out IList<EdmSchemaError> errors)
114 EDesignUtil.CheckTargetEntityFrameworkVersionArgument(targetEntityFrameworkVersion, "targetEntityFrameworkVersion");
115 return CreateStoreItemCollection(readers, out errors);
120 /// Create a StorageMappingItemCollection with the passed in parameters.
121 /// Add any errors caused during the ItemCollection creation
122 /// to the error list passed in.
124 /// <param name="edmCollection"></param>
125 /// <param name="storeCollection"></param>
126 /// <param name="readers"></param>
127 /// <param name="errors"></param>
128 /// <returns></returns>
129 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "edm")]
130 public static StorageMappingItemCollection CreateStorageMappingItemCollection(EdmItemCollection edmCollection,
131 StoreItemCollection storeCollection, IEnumerable<XmlReader> readers, out IList<EdmSchemaError> errors)
133 return new StorageMappingItemCollection(edmCollection, storeCollection, readers, null, out errors);
137 /// Create a StorageMappingItemCollection with the passed in parameters.
138 /// Add any errors caused during the ItemCollection creation
139 /// to the error list passed in.
141 /// <param name="edmCollection"></param>
142 /// <param name="storeCollection"></param>
143 /// <param name="readers"></param>
144 /// <param name="targetEntityFrameworkVersion"></param>
145 /// <param name="errors"></param>
146 /// <returns></returns>
147 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "edm")]
148 public static StorageMappingItemCollection CreateStorageMappingItemCollection(
149 EdmItemCollection edmCollection,
150 StoreItemCollection storeCollection,
151 IEnumerable<XmlReader> readers,
152 Version targetEntityFrameworkVersion,
153 out IList<EdmSchemaError> errors)
155 EDesignUtil.CheckArgumentNull(edmCollection, "edmCollection");
156 EDesignUtil.CheckArgumentNull(storeCollection, "storeCollection");
157 EDesignUtil.CheckArgumentNull(readers, "readers");
158 EDesignUtil.CheckTargetEntityFrameworkVersionArgument(targetEntityFrameworkVersion, "targetEntityFrameworkVersion");
159 if (EntityFrameworkVersionsUtil.ConvertToVersion(edmCollection.EdmVersion) > targetEntityFrameworkVersion)
161 throw EDesignUtil.Argument("edmCollection");
164 StorageMappingItemCollection storageMappingItemCollection = CreateStorageMappingItemCollection(edmCollection, storeCollection, readers, out errors);
165 if (!errors.Any(e => e.Severity == EdmSchemaErrorSeverity.Error))
167 ValidateActualVersionAgainstTarget(targetEntityFrameworkVersion, EntityFrameworkVersionsUtil.ConvertToVersion(storageMappingItemCollection.MappingVersion), errors);
169 return storageMappingItemCollection;