Asp.net mvc 5 在使用实体框架数据库时,如何在模型中写入属性
我正在使用asp.NETMVC5和EntityFramework6开发一个应用程序。我已经从数据库生成了模型类,并为模型类中的实体设置了一些属性。但每次刷新实体模型时,这些属性都会被清除。我知道这是因为模型类是从数据库生成的。那么,如何在entity frame work db first方法中指定其他属性?您需要指定一个元数据类。假设您的模型类是Asp.net mvc 5 在使用实体框架数据库时,如何在模型中写入属性,asp.net-mvc-5,entity-framework-6,Asp.net Mvc 5,Entity Framework 6,我正在使用asp.NETMVC5和EntityFramework6开发一个应用程序。我已经从数据库生成了模型类,并为模型类中的实体设置了一些属性。但每次刷新实体模型时,这些属性都会被清除。我知道这是因为模型类是从数据库生成的。那么,如何在entity frame work db first方法中指定其他属性?您需要指定一个元数据类。假设您的模型类是Employee。您将为Employee创建一个单独的分部类文件,并定义元数据类型 [MetadataType(typeof(EmployeeMeta
Employee
。您将为Employee
创建一个单独的分部类文件,并定义元数据类型
[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee
{
}
不要将上述属性添加到生成的Employee.cs
。创建一个单独的文件(例如EmployeePartial.cs
)
然后创建EmployeeMetadata类并定义所需的属性
public class EmployeeMetadata
{
[StringLength(100)]
public string LastName { get; set; }
}