Metadatatype属性不起作用 [英] Metadatatype attribute not working
问题描述
您好,
我正在使用MVC应用程序并使用了EF。 [数据库第一种方法]。
我从EF创建了脚手架模板。我有2个表tblEmployee和tblDepartment。将他们在edmx设计师中的名称更改为Employee and Department。
现在,当我进入索引视图时,向我显示所有员工,其中有两列名称为Name,一个是员工,一个是部门。所以现在我想将部门的名称名称更改为部门名称。
因为我使用DB第一种方法无法进行更改DataModel.Designer.cs
我创建了一个部分类Department [名称与Designer.cs中的相同]
用MetaDataType属性修饰它并传递一个带有name属性的类到类型参数。
一切都很好,直到现在。但是现在当我运行项目时,我没有在Index视图中看到更改。不知道该怎么做。我是MVC的新手。任何形式的帮助都很受欢迎!!!
我的尝试:
这是我创建的新的部分类
Hello,
i am working on an MVC application and have used EF. [Database first approach].
I created Scaffold Template from EF. I have 2 tables tblEmployee and tblDepartment. Changed their name in edmx designer to Employee and Department.
Now when i go to index view which shows me all the employees there are two columns with name "Name" , one for employee and one for department. So now i want to change the Column name "Name" for department to "Department Name".
As i am using DB first approach can't do the changes in DataModel.Designer.cs
SO i created a partial class Department [ name is same as in Designer.cs]
decorated it with MetaDataType attribute and passed a class with name property to the type parameter.
All good till now. But now when i run the project i don't see the changes in the Index view. don't know what to do now.I am New to MVC. any sort of help is much appriciated !!!
What I have tried:
This is the new partial class that i created
[MetadataType( typeof(DepartmentMetaData))]
public partial class Department
{
}
public class DepartmentMetaData
{
[Display(Name = "Department Name")]
public string Name { get; set; }
}
这是DataModel.Designer.cs中的属性
this is the property in DataModel.Designer.cs
[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();
}
}
因此属性名称匹配且有部分类。它现在应该在显示记录时更改名称,但事实并非如此。请告诉我,如果我错过了什么。
so the name of the property matches and there is a partial class . It should now change the name while showing records but it isn't. Please tell me if i missed something.
推荐答案
结果我的EDMX文件在我的模型文件夹之外,我在其中创建了我的Partial类。两者都需要在外面或两者都需要在文件夹内。我一改变班级的位置就开始工作了。 :)
Turns Out My EDMX file was outside my Model Folder in which i had created my Partial class. Either both needs to be outside or both needs to be inside the folder. As soon as i changed the location of my class it started working. :)
您创建的模型类和元类都应该具有相同的命名空间:
Your model class and meta class that you have created both should have same namespace like :
namespace AppName.Models
这篇关于Metadatatype属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!