[英]Modify Model structure without changing existing EF 4.x Generated Model in Database First Approach
I'm using ASP.NET MVC 3.0 Database First approach with EF 4.x. 我正在使用带有EF 4.x的ASP.NET MVC 3.0数据库优先方法。 Models are generated with EF 4.x DBContext Generator.
使用EF 4.x DBContext Generator生成模型。 Now I want to change model structure but don't want to change existing model because if database is modified I have to generate model again so the changes will be gone.
现在,我想更改模型结构,但不想更改现有模型,因为如果数据库被修改,我必须再次生成模型,这样更改将消失。 I think it should be done by overriding partial classes but not clear.
我认为应该通过重写部分类来完成,但不清楚。 Is there any example to do it?
有什么例子可以做到吗? what is the best way to implement it?
实施它的最佳方法是什么?
Edit: I have to add some additional validations in some Models, not all. 编辑:我必须在某些模型中添加一些附加验证,而不是全部。 I want to create independent models which overrides the generated classes and in case if EF models are regenerated there is no harm on our customized models.
我想创建覆盖生成的类的独立模型,并且如果重新生成EF模型,则对我们的自定义模型没有危害。
You can create partial classes like this 您可以像这样创建部分类
namespace MyProject.Data{ public partial class User{} } 命名空间MyProject.Data {公共局部类User {}}
but your partial class should be the same namespace of EF created classes 但是您的部分类应该与EF创建的类的名称空间相同
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(UserMetadata))]
public partial class User : IEntity
{
internal class UserMetadata
{
[DisplayName("User Name")]
public virtual string UserName
{
get;
set;
}
[DisplayName("First Name")]
public virtual string FirstName
{
get;
set;
}
[DisplayName("LastName")]
public virtual string LastName
{
get;
set;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.