本文整理汇总了C#中Microsoft.OData.Edm.Library.EdmModel.SetVocabularyAnnotation方法的典型用法代码示例。如果您正苦于以下问题:C# EdmModel.SetVocabularyAnnotation方法的具体用法?C# EdmModel.SetVocabularyAnnotation怎么用?C# EdmModel.SetVocabularyAnnotation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.OData.Edm.Library.EdmModel
的用法示例。
在下文中一共展示了EdmModel.SetVocabularyAnnotation方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetComputedAnnotation
public static void SetComputedAnnotation(EdmModel model, IEdmProperty target)
{
EdmUtil.CheckArgumentNull(model, "model");
EdmUtil.CheckArgumentNull(target, "target");
IEdmBooleanConstantExpression val = new EdmBooleanConstant(true);
IEdmValueTerm term = CoreVocabularyModel.ComputedTerm;
Debug.Assert(term != null, "term!=null");
EdmAnnotation annotation = new EdmAnnotation(target, term, val);
annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(annotation);
}
示例2: SetComputedAnnotation
private static void SetComputedAnnotation(EdmModel model, IEdmProperty target)
{
// when 'target' is <Key> property, V4's 'Computed' also has the meaning of OData V3's 'Identity'.
var val = new EdmBooleanConstant(value: true);
var annotation = new EdmAnnotation(target, CoreVocabularyModel.ComputedTerm, val);
annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(annotation);
}
示例3: EnumMemberExpressionDemo
private static void EnumMemberExpressionDemo()
{
Console.WriteLine("EnumMemberExpressionDemo");
var model = new EdmModel();
var personType = new EdmEntityType("TestNS", "Person");
model.AddElement(personType);
var pid = personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false);
personType.AddKeys(pid);
personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
var colorType = new EdmEnumType("TestNS2", "Color", true);
model.AddElement(colorType);
colorType.AddMember("Cyan", new EdmIntegerConstant(1));
colorType.AddMember("Blue", new EdmIntegerConstant(2));
var outColorTerm = new EdmTerm("TestNS", "OutColor", new EdmEnumTypeReference(colorType, true));
model.AddElement(outColorTerm);
var exp = new EdmEnumMemberExpression(
new EdmEnumMember(colorType, "Blue", new EdmIntegerConstant(2))
);
var annotation = new EdmAnnotation(personType, outColorTerm, exp);
annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(annotation);
ShowModel(model);
var ann = model.FindVocabularyAnnotations<IEdmValueAnnotation>(personType, "TestNS.OutColor").First();
var memberExp = (IEdmEnumMemberExpression)ann.Value;
foreach (var member in memberExp.EnumMembers)
{
Console.WriteLine(member.Name);
}
}
示例4: TestEnumMemberReferencingExtraEnumType
public void TestEnumMemberReferencingExtraEnumType()
{
const string csdl = @"<?xml version=""1.0"" encoding=""utf-8""?>
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
<edmx:DataServices>
<Schema Namespace=""TestNS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
<EntityType Name=""Person"">
<Key>
<PropertyRef Name=""Id"" />
</Key>
<Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"" />
<Property Name=""Name"" Type=""Edm.String"" />
<Annotation Term=""TestNS.OutColor"">
<EnumMember>TestNS2.Color/Blue TestNS2.Color/Cyan</EnumMember>
</Annotation>
</EntityType>
<Term Name=""OutColor"" Type=""TestNS2.Color"" />
</Schema>
<Schema Namespace=""TestNS2"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
<EnumType Name=""Color"" IsFlags=""true"">
<Member Name=""Cyan"" Value=""1"" />
<Member Name=""Blue"" Value=""2"" />
</EnumType>
</Schema>
</edmx:DataServices>
</edmx:Edmx>";
IEdmModel model;
IEnumerable<EdmError> errors;
IEnumerable<EdmError> validationErrors;
#region try build model
var edmModel = new EdmModel();
var personType = new EdmEntityType("TestNS", "Person");
edmModel.AddElement(personType);
var pid = personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false);
personType.AddKeys(pid);
personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
var colorType = new EdmEnumType("TestNS2", "Color", true);
edmModel.AddElement(colorType);
colorType.AddMember("Cyan", new EdmIntegerConstant(1));
colorType.AddMember("Blue", new EdmIntegerConstant(2));
var outColorTerm = new EdmTerm("TestNS", "OutColor", new EdmEnumTypeReference(colorType, true));
edmModel.AddElement(outColorTerm);
var exp = new EdmEnumMemberExpression(
new EdmEnumMember(colorType, "Blue", new EdmIntegerConstant(2)),
new EdmEnumMember(colorType, "Cyan", new EdmIntegerConstant(1))
);
var annotation = new EdmAnnotation(personType, outColorTerm, exp);
annotation.SetSerializationLocation(edmModel, EdmVocabularyAnnotationSerializationLocation.Inline);
edmModel.SetVocabularyAnnotation(annotation);
var stream = new MemoryStream();
Assert.IsFalse(edmModel.Validate(out errors));
using (var xw = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true }))
{
Assert.IsTrue(EdmxWriter.TryWriteEdmx(edmModel, xw, EdmxTarget.OData, out errors));
}
stream.Seek(0, SeekOrigin.Begin);
using (var sr = new StreamReader(stream))
{
Assert.AreEqual(csdl, sr.ReadToEnd());
}
#endregion
Assert.IsTrue(EdmxReader.TryParse(XmlReader.Create(new StringReader(csdl)), out model, out errors), "parsed");
Assert.IsFalse(model.Validate(out validationErrors));
TestEnumMember(model);
}
示例5: TestCoreIsLanguageDependentAnnotation
public void TestCoreIsLanguageDependentAnnotation()
{
EdmModel model = new EdmModel();
EdmEntityContainer container = new EdmEntityContainer("Ns", "Container");
var personType = new EdmEntityType("Ns", "Person");
var propertyId = personType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
personType.AddKeys(propertyId);
var structuralProperty = personType.AddStructuralProperty("Concurrency", EdmCoreModel.Instance.GetInt32(true));
model.AddElement(personType);
var entitySet = container.AddEntitySet("People", personType);
var stringTerm = new EdmTerm("Ns", "HomeAddress", EdmCoreModel.Instance.GetString(true));
model.AddElement(stringTerm);
model.AddElement(container);
IEdmValueTerm term = CoreVocabularyModel.IsLanguageDependentTerm;
IEdmBooleanConstantExpression trueExpression = new EdmBooleanConstant(true);
IEdmBooleanConstantExpression falseExpression = new EdmBooleanConstant(false);
EdmAnnotation personIdAnnotation = new EdmAnnotation(propertyId, term, trueExpression);
personIdAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(personIdAnnotation);
EdmAnnotation structuralPropertyAnnotation = new EdmAnnotation(structuralProperty, term, falseExpression);
structuralPropertyAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(structuralPropertyAnnotation);
EdmAnnotation stringTermAnnotation = new EdmAnnotation(stringTerm, term, trueExpression);
stringTermAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(stringTermAnnotation);
var idAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
var structuralPropertyAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
var stringTermAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(stringTerm, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
var entitySetAnnotation = model.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, term).FirstOrDefault();
Assert.AreEqual(true, idAnnotationValue);
Assert.AreEqual(false, structuralPropertyAnnotationValue);
Assert.AreEqual(true, stringTermAnnotationValue);
Assert.IsNull(entitySetAnnotation);
const string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""Ns"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
<EntityType Name=""Person"">
<Key>
<PropertyRef Name=""Id"" />
</Key>
<Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"">
<Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""true"" />
</Property>
<Property Name=""Concurrency"" Type=""Edm.Int32"">
<Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""false"" />
</Property>
</EntityType>
<Term Name=""HomeAddress"" Type=""Edm.String"">
<Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""true"" />
</Term>
<EntityContainer Name=""Container"">
<EntitySet Name=""People"" EntityType=""Ns.Person"" />
</EntityContainer>
</Schema>";
IEnumerable<EdmError> errors;
StringWriter sw = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = System.Text.Encoding.UTF8;
XmlWriter xw = XmlWriter.Create(sw, settings);
model.TryWriteCsdl(xw, out errors);
xw.Flush();
xw.Close();
var actual = sw.ToString();
Assert.AreEqual(expected, actual);
IEdmModel parsedModel;
IEnumerable<EdmError> errs;
bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(expected)) }, out parsedModel, out errs);
Assert.IsTrue(parsed, "parsed");
Assert.IsTrue(!errors.Any(), "No errors");
var idAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, term).FirstOrDefault();
var propertyAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, term).FirstOrDefault();
var termAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(stringTerm, term).FirstOrDefault();
entitySetAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, term).FirstOrDefault();
Assert.AreEqual(null, propertyAnnotation);
Assert.AreEqual(null, idAnnotation);
Assert.AreEqual(null, termAnnotation);
Assert.AreEqual(null, entitySetAnnotation);
var parsedPersonType = parsedModel.FindType("Ns.Person") as IEdmEntityType;
idAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedPersonType.FindProperty("Id"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
structuralPropertyAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedPersonType.FindProperty("Concurrency"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
stringTermAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedModel.FindValueTerm("Ns.HomeAddress"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
entitySetAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedModel.FindDeclaredEntitySet("People"), term).FirstOrDefault();
Assert.IsNotNull(parsedPersonType);
Assert.AreEqual(false, structuralPropertyAnnotationValue);
//.........这里部分代码省略.........
示例6: AnnotationValueAsEnumMemberReferenceExpressionSerializationTest
public void AnnotationValueAsEnumMemberReferenceExpressionSerializationTest()
{
EdmModel model = new EdmModel();
EdmEntityContainer container = new EdmEntityContainer("Ns", "Container");
var personType = new EdmEntityType("Ns", "Person");
var propertyId = personType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
personType.AddKeys(propertyId);
var structuralProperty = personType.AddStructuralProperty("Concurrency", EdmCoreModel.Instance.GetInt32(true));
model.AddElement(personType);
var entitySet = container.AddEntitySet("People", personType);
var permissionType = new EdmEnumType("Ns", "Permissions", true);
var read = permissionType.AddMember("Read", new EdmIntegerConstant(1));
var write = permissionType.AddMember("Write", new EdmIntegerConstant(2));
var delete = permissionType.AddMember("Delete", new EdmIntegerConstant(4));
model.AddElement(permissionType);
var enumTerm = new EdmTerm("Ns", "Permission", new EdmEnumTypeReference(permissionType, false));
model.AddElement(enumTerm);
model.AddElement(container);
EdmAnnotation personIdAnnotation = new EdmAnnotation(propertyId, enumTerm, new EdmEnumMemberReferenceExpression(read));
personIdAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(personIdAnnotation);
EdmAnnotation structuralPropertyAnnotation = new EdmAnnotation(structuralProperty, enumTerm, new EdmEnumMemberExpression(read, write));
structuralPropertyAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(structuralPropertyAnnotation);
EdmAnnotation entitySetAnnotation = new EdmAnnotation(entitySet, enumTerm, new EdmEnumMemberReferenceExpression(delete));
structuralPropertyAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(entitySetAnnotation);
var idAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, enumTerm).FirstOrDefault().Value as IEdmEnumMemberReferenceExpression).ReferencedEnumMember;
var structuralPropertyAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, enumTerm).FirstOrDefault().Value as IEdmEnumMemberExpression).EnumMembers;
var entitySetAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, enumTerm).FirstOrDefault().Value as IEdmEnumMemberReferenceExpression).ReferencedEnumMember;
var enumTypeAnnotationValue = model.FindVocabularyAnnotations<IEdmValueAnnotation>(permissionType, enumTerm).FirstOrDefault();
Assert.AreEqual(read, idAnnotationValue);
Assert.AreEqual(2, structuralPropertyAnnotationValue.Count());
Assert.AreEqual(delete, entitySetAnnotationValue);
Assert.IsNull(enumTypeAnnotationValue);
const string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""Ns"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
<EntityType Name=""Person"">
<Key>
<PropertyRef Name=""Id"" />
</Key>
<Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"">
<Annotation Term=""Ns.Permission"">
<EnumMember>Ns.Permissions/Read</EnumMember>
</Annotation>
</Property>
<Property Name=""Concurrency"" Type=""Edm.Int32"">
<Annotation Term=""Ns.Permission"">
<EnumMember>Ns.Permissions/Read Ns.Permissions/Write</EnumMember>
</Annotation>
</Property>
</EntityType>
<EnumType Name=""Permissions"" IsFlags=""true"">
<Member Name=""Read"" Value=""1"" />
<Member Name=""Write"" Value=""2"" />
<Member Name=""Delete"" Value=""4"" />
</EnumType>
<Term Name=""Permission"" Type=""Ns.Permissions"" Nullable=""false"" />
<EntityContainer Name=""Container"">
<EntitySet Name=""People"" EntityType=""Ns.Person"" />
</EntityContainer>
<Annotations Target=""Ns.Container/People"">
<Annotation Term=""Ns.Permission"">
<EnumMember>Ns.Permissions/Delete</EnumMember>
</Annotation>
</Annotations>
</Schema>";
IEnumerable<EdmError> errors;
StringWriter sw = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = System.Text.Encoding.UTF8;
XmlWriter xw = XmlWriter.Create(sw, settings);
model.TryWriteCsdl(xw, out errors);
xw.Flush();
xw.Close();
var actual = sw.ToString();
Assert.IsTrue(!errors.Any(), "No errors");
Assert.AreEqual(expected, actual);
}