/// <summary> /// Shared code for adding a EdmTypeAttribute derived attribute including parameters to a type or property /// </summary> /// <param name="attributeName">Unqualified name of the attribute</param> /// <param name="type">The type or property type of the code that is having the attribute attached.</param> /// <param name="member">The type declaration to add the attribues to.</param> public void EmitSchemaTypeAttribute(string attributeName, EdmType type, CodeTypeMember member) { Debug.Assert(attributeName != null, "attributeName should not be null"); Debug.Assert(type != null, "type should not be null"); Debug.Assert(member != null, "typeDecl should not be null"); // [mappingattribute(SchemaName="namespace",TypeName="classname") CodeAttributeDeclaration attribute = EmitSimpleAttribute(attributeName); AttributeEmitter.AddNamedAttributeArguments(attribute, "NamespaceName", type.NamespaceName, "Name", type.Name); member.CustomAttributes.Add(attribute); }
/// <summary> /// The method to be called to create the type level attributes for the ItemTypeEmitter /// </summary> /// <param name="emitter">The strongly typed emitter</param> /// <param name="typeDecl">The type declaration to add the attribues to.</param> public void EmitTypeAttributes(EntityTypeEmitter emitter, CodeTypeDeclaration typeDecl) { Debug.Assert(emitter != null, "emitter should not be null"); Debug.Assert(typeDecl != null, "typeDecl should not be null"); EmitSchemaTypeAttribute(FQAdoFrameworkDataClassesName("EdmEntityTypeAttribute"), emitter, typeDecl); CodeAttributeDeclaration attribute2 = EmitSimpleAttribute("System.Runtime.Serialization.DataContractAttribute"); AttributeEmitter.AddNamedAttributeArguments(attribute2, "IsReference", true); typeDecl.CustomAttributes.Add(attribute2); CodeAttributeDeclaration attribute3 = EmitSimpleAttribute("System.Serializable"); typeDecl.CustomAttributes.Add(attribute3); EmitKnownTypeAttributes(emitter.Item, emitter.Generator, typeDecl); }
/// <summary> /// The method to be called to create the property level attributes for the PropertyEmitter /// </summary> /// <param name="emitter">The strongly typed emitter</param> /// <param name="propertyDecl">The type declaration to add the attribues to.</param> /// <param name="additionalAttributes">Additional attributes to emit</param> public void EmitPropertyAttributes(PropertyEmitter emitter, CodeMemberProperty propertyDecl, List <CodeAttributeDeclaration> additionalAttributes) { if (MetadataUtil.IsPrimitiveType(emitter.Item.TypeUsage.EdmType) || MetadataUtil.IsEnumerationType(emitter.Item.TypeUsage.EdmType)) { CodeAttributeDeclaration scalarPropertyAttribute = EmitSimpleAttribute(FQAdoFrameworkDataClassesName("EdmScalarPropertyAttribute")); if (emitter.IsKeyProperty) { Debug.Assert(emitter.Item.Nullable == false, "An EntityKeyProperty cannot be nullable."); AttributeEmitter.AddNamedAttributeArguments(scalarPropertyAttribute, "EntityKeyProperty", true); } if (!emitter.Item.Nullable) { AttributeEmitter.AddNamedAttributeArguments(scalarPropertyAttribute, "IsNullable", false); } propertyDecl.CustomAttributes.Add(scalarPropertyAttribute); } else //Complex property { Debug.Assert(MetadataUtil.IsComplexType(emitter.Item.TypeUsage.EdmType) || (MetadataUtil.IsCollectionType(emitter.Item.TypeUsage.EdmType)), "not a complex type or a collection type"); CodeAttributeDeclaration attribute = EmitSimpleAttribute(FQAdoFrameworkDataClassesName("EdmComplexPropertyAttribute")); propertyDecl.CustomAttributes.Add(attribute); // Have CodeDOM serialization set the properties on the ComplexObject, not the ComplexObject instance. attribute = EmitSimpleAttribute("System.ComponentModel.DesignerSerializationVisibility"); AttributeEmitter.AddAttributeArguments(attribute, new object[] { new CodePropertyReferenceExpression( new CodeTypeReferenceExpression(TypeReference.ForType( typeof(System.ComponentModel.DesignerSerializationVisibility))), "Content") }); propertyDecl.CustomAttributes.Add(attribute); if (!MetadataUtil.IsCollectionType(emitter.Item.TypeUsage.EdmType)) { // Non-collection complex properties also need additional serialization attributes to force them to be explicitly serialized if they are null // If this is omitted, null complex properties do not get explicitly set to null during deserialization, which causes // them to be lazily constructed once the property is accessed after the entity is deserialized. If the property is // actually null during serialiation, that means the user has explicitly set it, so we need to maintain that during serialization. // This doesn't apply to collection types because they aren't lazily constructed and don't need this extra information. attribute = EmitSimpleAttribute("System.Xml.Serialization.XmlElement"); AttributeEmitter.AddNamedAttributeArguments(attribute, "IsNullable", true); propertyDecl.CustomAttributes.Add(attribute); attribute = EmitSimpleAttribute("System.Xml.Serialization.SoapElement"); AttributeEmitter.AddNamedAttributeArguments(attribute, "IsNullable", true); propertyDecl.CustomAttributes.Add(attribute); } } // serialization attribute AddDataMemberAttribute(propertyDecl); if (additionalAttributes != null && additionalAttributes.Count > 0) { try { propertyDecl.CustomAttributes.AddRange(additionalAttributes.ToArray()); } catch (ArgumentNullException e) { emitter.Generator.AddError(Strings.InvalidAttributeSuppliedForProperty(emitter.Item.Name), ModelBuilderErrorCode.InvalidAttributeSuppliedForProperty, EdmSchemaErrorSeverity.Error, e); } } EmitGeneratedCodeAttribute(propertyDecl); }