实体框架数据库首先恢复让我失去手动更改 [英] Entity Framework Database First regeneration make me lose manual changes

查看:88
本文介绍了实体框架数据库首先恢复让我失去手动更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个网站,MVC .NET。

I'm making a website with MVC .NET.

由于我是一个老派的程序员谁学会先设计数据库时,我选择了数据库的第一种方法。我还使用code一代,这与延伸 .TT 创建文件。一切正常,到目前为止除了一个东西,我botters

Since I'm an old school programmer who learn to design database first, I opted for the Database first approach. I'm also using "code generation" which create files with the extension .tt . Everything is working so far except one things that botters me.

经典场景:


  • 我意识到我缺少一个字段

  • 我加入数据库中的字段。

  • 我进入EDMX并选择更新从数据库模型。

然后我回到我的code和使用一样,我把模型场的顶部被拆除的特殊显示名称标签工作的事情。

Then I go back to my code and things that use to work like special DisplayName tag that I put on top of model field were removed.

例如,如果我有这样的:

For example if I have this:

public partial class Blog
    {
        public Blog()
        {
            this.BlogComments = new HashSet<BlogComment>();
        }

        public int IDBlog { get; set; }
        public string Title { get; set; }

        [AllowHtml]
        public string Content { get; set; }
        public System.DateTime DateCreated { get; set; }
        public string Author { get; set; }

        public virtual ICollection<BlogComment> BlogComments { get; set; }
    }

将成为

public partial class Blog
    {
        public Blog()
        {
            this.BlogComments = new HashSet<BlogComment>();
        }

        public int IDBlog { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public System.DateTime DateCreated { get; set; }
        public string Author { get; set; }

        public virtual ICollection<BlogComment> BlogComments { get; set; }
    }

这是因为 [AllowHtml] 是previous代模型后加入。有没有一种方法来更新表,而不是删除所有我产生后添加标签?我该怎么办呢?

That is because the [AllowHtml] was added after a previous generation of the model. Is there a way to update the table and not remove all the tag that I add after the generation? How can I do that?

现在,我做一些恢复使用SVN,但它很快就会unmanagable进行管理。

Right now, I manage this by doing some revert with SVN but it will soon be unmanagable.

感谢

推荐答案

不要编辑生成的文件。永远。只是。别。做。它

Don't edit the generated files. Ever. Just. Don't. Do. It.

相反,使部分文件的编辑在不同的目录。要在你的部分类定义的顶部添加属性,声明一个元数据类。

Instead, make your edits in partial files in a different directory. To add attributes, declare a Metadata class at the top of your partial class definition.

[MetadataType(typeof(BlogMetadata))]
public partial class Blog
{
    // it's possible to add logic and non-mapped properties here
}

现在在你的元数据类,你可以定义属性或其它逻辑:

Now in your Metadata class, you can define attributes or other logic:

public class BlahMetadata
{
    [AllowHtml] 
    public string Content{ get; set; } 
}

这篇关于实体框架数据库首先恢复让我失去手动更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆