DigitalDNA
DigitalDNA

Reputation: 71

How to Add a read-only Property to Database first Entity Framework 6 metadata

i'm learning MVC and EF6 on my own, actually i'm following the tutorials on asp.net site that are very helpful. Today i was following this tutorial. This is with code-first approach, but i'm doing this with database first approach. so i've followed the instructions to add the following read-only property to the Model, in my case to the model's metadata.

[Display(Name = "Full Name")]
public string FullName
{
      get
      {
         return LastName + ", " + FirstMidName;
      }
}

but when i try to Access the students page i got this error:

An exception of type 'System.InvalidOperationException' occurred in System.ComponentModel.DataAnnotations.dll but was not handled in user code. Additional information: The associated metadata type for type 'MVCEF6DBFirst.Models.Student' contains the following unknown properties or fields: FullName. Ensure that the names of these members match the names of the properties of the main type.

I know that there is an error with the property recently added so i googled it but i can't find a correct solution to this.

Could you tell me how i should add this property to my model

thanks in advance.

Upvotes: 0

Views: 3938

Answers (2)

DigitalDNA
DigitalDNA

Reputation: 71

As i commented in the @sergik's answer i've found another approach to get this work (based on @sergik's answer).

  1. EF Model Class


public class StudentMetadata
{                

}

  1. Declare a partial class


[MetadataType(typeof(StudentMetadata))]    
    public partial class Student    
    {    
       [Display(Name = "Full Name")]    
       public string FullName    
       {    
          get    
          {    
             return LastName + ", " + FirstName;    
          }    
       }    
    }

  1. add DataAnnotations reference to this class


    using System.ComponentModel.DataAnnotations;

  1. Run Project. This is the result Result addindg a read-only property to student class Database-First EF6

Both @Sergik's and me answers were tested and both Works well.

Hope to be useful for other people.

Upvotes: 0

sergik
sergik

Reputation: 111

If the FullName column doesn't exist in the database table which means no property FullName exists in your EF model Student class you'll get this error.

  1. I doubt you can achieve what you want using metadata-approach. But you can modify the way your properties are rendered in a view. If the following is your EF model class:

    public partial class Student
    {   
      public string FullName 
      {             
        get;
        set;
      }
    }
    
  2. Then create another partial class called Student and place it in the same assembly(folder) but in a different file with a different file name. it'd look like this:

    [MetadataType(typeof(StudentMetadata))]
    public partial class Student
    {
    }
    
    public class StudentMetadata
    {
        [Display(Name = "Full Name")]
        public string FullName { get; set; }          
    } 
    

then if you render your EF model Student class in a view

@model Models.Student
@Html.DisplayForModel()

The name of the property will be displayed the way you wanted:

If you want to prevent user from editing the field by making it readonly, you may want to use this

 @Html.TextBoxFor(m => m.FullName, new{ disabled = "disabled", @readonly = "readonly" })

I am not sure how to achieve the same effect with Html.EditorForModel

Alternatively if you still want a property FullName made up of other two properties FirstName and LastName I'd try:

  1. Your EF model class:

    public partial class Student
    {
     public string FirstName
     {
         get; set;
     }
    
     public string SecondName
     {
        get; set;
     }
    

    }

  2. Class extending Student class:

    public class StudentChild : Student
    {
    
    [Display(Name="User name")]
    public String FullName
    {
        get { return FirstName + ", " + SecondName; }
    }
    

    }

  3. Then in your view:

    @model StudentChild
    
    @Html.DisplayForModel()
    

I am not sure the latter is the perfect approach as in the case that your underlying database schema changes then you need to modify your child class.

Upvotes: 1

Related Questions