Monday 30 December 2013

Map a MetadataType to the Entity Framework Model - ASP.NET MVC

In this article we are going to see how to map a MetadataType to the Model class for applying the additonal rules and conditions.In the following samples we are created the model using entity framework.

Model created by EntityFramwork :

[EdmEntityTypeAttribute(NamespaceName="EmployeeModel", Name="Department")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Department : EntityObject
    {
        #region Factory Method
   
        /// <summary>
        /// Create a new Department object.
        /// </summary>
        /// <param name="id">Initial value of the ID property.</param>
        public static Department CreateDepartment(global::System.Int32 id)
        {
            Department department = new Department();
            department.ID = id;
            return department;
        }

        #endregion
        #region Primitive Properties
   
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 ID
        {
            get
            {
                return _ID;
            }
            set
            {
                if (_ID != value)
                {
                    OnIDChanging(value);
                    ReportPropertyChanging("ID");
                    _ID = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("ID");
                    OnIDChanged();
                }
            }
        }
        private global::System.Int32 _ID;
        partial void OnIDChanging(global::System.Int32 value);
        partial void OnIDChanged();
   
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String NAME
        {
            get
            {
                return _NAME;
            }
            set
            {
                OnNAMEChanging(value);
                ReportPropertyChanging("NAME");
                _NAME = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("NAME");
                OnNAMEChanged();
            }
        }
        private global::System.String _NAME;
        partial void OnNAMEChanging(global::System.String value);
        partial void OnNAMEChanged();

        #endregion
   
        #region Navigation Properties
   
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [XmlIgnoreAttribute()]
        [SoapIgnoreAttribute()]
        [DataMemberAttribute()]
        [EdmRelationshipNavigationPropertyAttribute("EmployeeModel", "FK__EMPTABLE__DEPTID__1A14E395", "EMPTABLE")]
        public EntityCollection<Employee> Employees
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Employee>("EmployeeModel.FK__EMPTABLE__DEPTID__1A14E395", "EMPTABLE");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Employee>("EmployeeModel.FK__EMPTABLE__DEPTID__1A14E395", "EMPTABLE", value);
                }
            }
        }

        #endregion
    }
   
  


MetadataType:

[MetadataType(typeof(DepartmentMetadata))]
    public partial class Department
    {

    }

    public class DepartmentMetadata
    {
        [Display(Name="Department Name")]
        public string NAME { set; get; }
    }



In this we need to change the display name of a Property, so we can see how to do this .



HTML :

@model EmpApp.Models.Employee

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>Employee</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.NAME)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.NAME)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Department.NAME)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Department.NAME)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.COUNTRY)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.COUNTRY)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.MARRIED)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.MARRIED)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>







2 comments: