Пример #1
0
        /// <summary>
        /// Tries parsing the given EDMX artifact for an IEdmModel.
        /// </summary>
        /// <param name="reader">XmlReader containing the EDMX artifact.</param>
        /// <param name="references">Models to be referenced by the created model.</param>
        /// <param name="settings">EdmxReader settings for current parser.</param>
        /// <param name="model">The model generated by parsing</param>
        /// <param name="errors">Errors reported while parsing.</param>
        /// <remarks>If getReferencedModelReaderFunc throws exception, it won't be caught internally but will be thrown out for caller to handle.</remarks>
        /// <returns>Success of the parse operation.</returns>
        /// <remarks>
        /// User should handle the disposal of XmlReader created by getReferencedModelReaderFunc.
        /// </remarks>
        public static bool TryParse(XmlReader reader, IEnumerable<IEdmModel> references, EdmxReaderSettings settings, out IEdmModel model, out IEnumerable<EdmError> errors)
        {
            EdmUtil.CheckArgumentNull(references, "references");
            if (settings == null)
            {
                return TryParse(reader, references, out model, out errors);
            }

            EdmxReader edmxReader = new EdmxReader(reader, settings.GetReferencedModelReaderFunc)
            {
                ignoreUnexpectedAttributesAndElements = settings.IgnoreUnexpectedAttributesAndElements
            };
            return edmxReader.TryParse(references, out model, out errors);
        }
Пример #2
0
        /// <summary>
        /// Tries parsing the given EDMX artifact for an IEdmModel.
        /// </summary>
        /// <param name="reader">XmlReader containing the EDMX artifact.</param>
        /// <param name="references">Models to be referenced by the created model.</param>
        /// <param name="settings">EdmxReader settings for current parser.</param>
        /// <param name="model">The model generated by parsing</param>
        /// <param name="errors">Errors reported while parsing.</param>
        /// <remarks>If getReferencedModelReaderFunc throws exception, it won't be caught internally but will be thrown out for caller to handle.</remarks>
        /// <returns>Success of the parse operation.</returns>
        /// <remarks>
        /// User should handle the disposal of XmlReader created by getReferencedModelReaderFunc.
        /// </remarks>
        public static bool TryParse(XmlReader reader, IEnumerable <IEdmModel> references, EdmxReaderSettings settings, out IEdmModel model, out IEnumerable <EdmError> errors)
        {
            EdmUtil.CheckArgumentNull(references, "references");
            if (settings == null)
            {
                return(TryParse(reader, references, out model, out errors));
            }

            EdmxReader edmxReader = new EdmxReader(reader, settings.GetReferencedModelReaderFunc)
            {
                ignoreUnexpectedAttributesAndElements = settings.IgnoreUnexpectedAttributesAndElements
            };

            return(edmxReader.TryParse(references, out model, out errors));
        }
Пример #3
0
        public void ParsingReferenceModelWithAnnotationsInReferenceShouldThrowExceptionAccordingToIgnoreUnexpectedAttributesAndElements()
        {
            var edmx =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
  <edmx:Reference Uri=""http://host/schema/Location.xml"">
    <Annotation Term=""Core.Description"">
      <String>Core terms needed to write vocabularies</String>
    </Annotation>
    <edmx:Include Namespace=""NS.Ref1"" Alias=""Location"" />
    <Annotation Term=""Core.Description"" />
    <Annotation Term=""Core.Description"">
    </Annotation>
  </edmx:Reference>
  <edmx:DataServices>
    <Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
        <EntityType Name=""Customer"">
            <Key>
            <PropertyRef Name=""CustomerID"" />
            </Key>
            <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
        </EntityType>
        <EntityContainer Name=""C1"">
            <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
            <EntitySet Name=""VipCustomers"" EntityType=""Location.VipCustomer"" />
        </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            // the above EntitySet 'VipCustomers' will reference the below EntityType 'VipCustomer' ,
            // and the below NS1.Address reference the above complex type.
            var edmxRef1 =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
  <edmx:DataServices>
    <Schema Namespace=""NS.Ref1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <EntityType Name=""VipCustomer"">
        <Key>
        <PropertyRef Name=""VipCustomerID""/>
        </Key>
        <Property Name=""VipCustomerID"" Type=""Edm.String"" Nullable=""false"" />
    </EntityType>
    <EntityContainer Name=""Vip_C1"">
        <EntitySet Name=""VipCustomers"" EntityType=""NS1.VipCustomer"" />
    </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            Func<Uri, XmlReader> getReferencedModelReaderFunc = uri =>
            {
                if (uri.AbsoluteUri == "http://host/schema/Location.xml")
                {
                    return XmlReader.Create(new StringReader(edmxRef1));
                }

                return null;
            };

            IEdmModel model;
            IEnumerable<EdmError> errors;
            EdmxReaderSettings settings = new EdmxReaderSettings()
            {
                GetReferencedModelReaderFunc = getReferencedModelReaderFunc,
                IgnoreUnexpectedAttributesAndElements = true
            };
            bool parsed = EdmxReader.TryParse(XmlReader.Create(new StringReader(edmx)), Enumerable.Empty<IEdmModel>(), settings, out model, out errors);
            Assert.AreEqual(true, parsed);
            Assert.AreEqual(3, errors.Count());
            Assert.IsNotNull(model.FindType("NS.Ref1.VipCustomer"), "referenced type should be found");
            IEdmEntitySet entitySet = model.EntityContainer.EntitySets().First<IEdmEntitySet>(s => s.Name == "VipCustomers");
            Assert.IsNotNull(entitySet, "should not be null");
            Assert.IsNotNull(entitySet.EntityType(), "should not be null");

            settings = new EdmxReaderSettings()
            {
                GetReferencedModelReaderFunc = getReferencedModelReaderFunc,
                IgnoreUnexpectedAttributesAndElements = false
            };
            parsed = EdmxReader.TryParse(XmlReader.Create(new StringReader(edmx)), Enumerable.Empty<IEdmModel>(), settings, out model, out errors);
            Assert.AreEqual(false, parsed);
            Assert.AreEqual(3, errors.Count());
        }
Пример #4
0
        public void EdmxReaderEntitysEnumKeyTest()
        {
            var edmx =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
  <edmx:DataServices>
    <Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
      <EnumType IsFlags=""true"" Name=""Gender"">
        <Member Name=""Female"" Value=""2"" /> 
        <Member Name=""Male"" Value=""1"" /> 
      </EnumType>
        <EntityType Name=""Family"">
            <Key>
            <PropertyRef Name=""GenderVal"" />
            </Key>
            <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
            <Property Name=""GenderVal"" Type=""NS1.Gender"" Nullable=""false"" />
        </EntityType>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            Func<Uri, XmlReader> getReferencedModelReaderFunc = uri =>
            {
                return null;
            };

            IEdmModel model;
            IEnumerable<EdmError> errors;
            EdmxReaderSettings settings = new EdmxReaderSettings()
            {
                GetReferencedModelReaderFunc = getReferencedModelReaderFunc,
                IgnoreUnexpectedAttributesAndElements = true
            };

            bool parsed = EdmxReader.TryParse(XmlReader.Create(new StringReader(edmx)), new IEdmModel[0], settings, out model, out errors);
            Assert.AreEqual(true, parsed);
            Assert.AreEqual(0, errors.Count());
            IEnumerable<EdmError> validationResults;
            bool valid = model.Validate(out validationResults);
            Assert.IsTrue(valid);
            Assert.AreEqual(0, validationResults.Count());
        }
Пример #5
0
        public void EdmxReaderSettingsTest()
        {
            var edmx =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
  <edmx:Reference Uri=""http://host/schema/Location.xml"">
    <edmx:Include Namespace=""NS.Ref1"" Alias=""Location"" />
  </edmx:Reference>
  <edmx:DataServices>
    <Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
        <EntityType Name=""Customer"">
            <Key>
            <PropertyRef Name=""CustomerID"" />
            </Key>
            <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
            <Property Name=""Property1"" Type=""Location.C1"" Nullable=""false"" />
            <Property Name=""Property2"" Type=""NS.Ref2.C2"" Nullable=""false"" />
        </EntityType>
        <EntityContainer Name=""C1"">
            <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
        </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            var edmxRef1 =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
  <edmx:DataServices>
    <Schema Namespace=""NS.Ref1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <ComplexType Name=""C1"" />
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            Func<Uri, XmlReader> getReferencedModelReaderFunc = uri =>
            {
                if (uri.AbsoluteUri == "http://host/schema/Location.xml")
                {
                    return XmlReader.Create(new StringReader(edmxRef1));
                }

                return null;
            };

            IEdmModel model;
            IEnumerable<EdmError> errors;
            EdmxReaderSettings settings = new EdmxReaderSettings()
            {
                GetReferencedModelReaderFunc = getReferencedModelReaderFunc,
                IgnoreUnexpectedAttributesAndElements = true
            };

            EdmModel ref2 = new EdmModel();
            var c2Type = new EdmComplexType("NS.Ref2", "C2");
            ref2.AddElement(c2Type);

            bool parsed = EdmxReader.TryParse(XmlReader.Create(new StringReader(edmx)), new[] { ref2 }, settings, out model, out errors);
            Assert.AreEqual(true, parsed);
            Assert.AreEqual(0, errors.Count());
            var customerType = model.FindType("NS1.Customer") as IEdmEntityType;
            Assert.IsNotNull(customerType);
            var property1 = customerType.FindProperty("Property1");
            var c1Type = model.FindType("NS.Ref1.C1");
            Assert.IsNotNull(c1Type);
            Assert.AreEqual(c1Type, property1.Type.Definition);

            var property2 = customerType.FindProperty("Property2");
            Assert.IsNotNull(c2Type);
            Assert.AreEqual(c2Type, property2.Type.Definition);
        }
Пример #6
0
        public void ParsingReferenceModelWithFuncReturnNullPassedWhenParsingWellKnownUriShouldNotThrown()
        {
            var edmx =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
  <edmx:Reference Uri=""http://docs.oasis-open.org/odata/odata/v4.0/os/vocabularies/Org.OData.Core.V1.xml"">
    <edmx:Include Namespace=""NS.Ref1"" Alias=""Location"" />
  </edmx:Reference>
  <edmx:DataServices>
    <Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
        <EntityType Name=""Customer"">
            <Key>
            <PropertyRef Name=""CustomerID"" />
            </Key>
            <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
        </EntityType>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            IEdmModel model;
            IEnumerable<EdmError> errors;
            EdmxReaderSettings settings = new EdmxReaderSettings()
            {
                GetReferencedModelReaderFunc = (uri) => null,
                IgnoreUnexpectedAttributesAndElements = false
            };
            bool parsed = EdmxReader.TryParse(XmlReader.Create(new StringReader(edmx)), Enumerable.Empty<IEdmModel>(), settings, out model, out errors);
            Assert.AreEqual(true, parsed);
            Assert.AreEqual(0, errors.Count());
            Assert.AreEqual(4, model.ReferencedModels.Count());
        }
Пример #7
0
        public void ParsingReferenceModelWithFuncReturnNullPassedShouldThrow()
        {
            var edmx =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
  <edmx:Reference Uri=""http://host/schema/Location.xml"">
    <edmx:Include Namespace=""NS.Ref1"" Alias=""Location"" />
  </edmx:Reference>
  <edmx:DataServices>
    <Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
        <EntityType Name=""Customer"">
            <Key>
            <PropertyRef Name=""CustomerID"" />
            </Key>
            <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
        </EntityType>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            IEdmModel model;
            IEnumerable<EdmError> errors;
            EdmxReaderSettings settings = new EdmxReaderSettings()
            {
                GetReferencedModelReaderFunc = (uri) => null,
                IgnoreUnexpectedAttributesAndElements = false
            };
            bool parsed = EdmxReader.TryParse(XmlReader.Create(new StringReader(edmx)), Enumerable.Empty<IEdmModel>(), settings, out model, out errors);
            Assert.AreEqual(false, parsed);
            Assert.AreEqual(1, errors.Count());
            var error = errors.Single();
            // TODO: Fix Reference error location
            // Assert.AreEqual("(3, 19)", error.ErrorLocation.ToString());
            Assert.AreEqual(EdmErrorCode.UnresolvedReferenceUriInEdmxReference, error.ErrorCode);
            Assert.AreEqual(Strings.EdmxParser_UnresolvedReferenceUriInEdmxReference, error.ErrorMessage);
        }