Reputation: 71
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
Reputation: 71
As i commented in the @sergik's answer i've found another approach to get this work (based on @sergik's answer).
public class StudentMetadata
{
}
[MetadataType(typeof(StudentMetadata))]
public partial class Student
{
[Display(Name = "Full Name")]
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
}
using System.ComponentModel.DataAnnotations;
Both @Sergik's and me answers were tested and both Works well.
Hope to be useful for other people.
Upvotes: 0
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.
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;
}
}
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:
Your EF model class:
public partial class Student
{
public string FirstName
{
get; set;
}
public string SecondName
{
get; set;
}
}
Class extending Student class:
public class StudentChild : Student
{
[Display(Name="User name")]
public String FullName
{
get { return FirstName + ", " + SecondName; }
}
}
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