简体   繁体   English

修改模型结构而不更改数据库优先方法中现有的EF 4.x生成的模型

[英]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模型,则对我们的自定义模型没有危害。

  1. you can change the *.tt file for generating objects from model. 您可以更改* .tt文件以从模型生成对象。 What do you want to change objects. 您要更改什么对象。
  2. 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.

相关问题 EF4 Database First Model 基于正确规范化表的设计 - EF4 Database First Model Design based on properly normalized table 代码优先生成的数据库与DbContext模型不同 - Code-First generated database different from DbContext Model 使用数据库首先生成带有外键的MVC 4模型时遇到问题 - Having trouble with database first generated MVC 4 model with foreign keys EF 5-模型首次数据库迁移:发布到远程生产服务器后要更改模型? - EF 5 - Model first database migration: change model after published to remote production server? 在现有项目中首先使用 EF 代码首先使用 EF 数据库 - use EF code first in an existing project works with EF database first 为什么POCO类为C#生成的EF 4.x DbContext生成器是局部的? - Why POCO Classes generated EF 4.x DbContext Generator for C# are Partial? 从数据库更新EF模型导致现有项目正常运行的多重性错误 - Update EF model from database is causing Multiplicity errors on an existing project working perfectly appharbor [Win32Exception(0x80004005):系统找不到指定的文件],其模型为EF - appharbor [Win32Exception (0x80004005): The system cannot find the file specified] with model first EF 内联编辑和更新gridview数据并将其保存到实体模型数据库的第一种方法 - Inline edit and update gridview data and saved it to the entity model database first approach 更改asp.net mvc模型的EF Code First模型后,如何在Azure中正确更新SQL数据库? - How to properly update SQL database in Azure after EF Code First model of asp.net mvc model change?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM