实体框架数据库首次重新生成让我失去手动更改

Jea*_*ôté 3 c# entity-framework-4 asp.net-mvc-3 database-first

我正在建立一个MVC .NET网站.

由于我是一名学习首先设计数据库的老派程序员,因此我选择了数据库第一种方法.我也在使用"代码生成"来创建扩展名的文件.tt.到目前为止,一切都在发挥作用,除了一件令我烦恼的事情.

经典场景:

  • 我意识到我错过了一个领域
  • 我在数据库中添加了该字段.
  • 我进入edmx并选择从数据库更新模型.

然后我回到我的代码和那些用于工作的东西,就像我放在模型字段顶部的特殊DisplayName标签一样被删除了.

例如,如果我有这个:

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; }
    }
Run Code Online (Sandbox Code Playgroud)

它会变成

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; }
    }
Run Code Online (Sandbox Code Playgroud)

那是因为在[AllowHtml]上一代模型之后添加了.有没有办法更新表,而不是删除生成后添加的所有标记?我怎样才能做到这一点?

现在,我通过使用SVN进行一些还原来管理这个,但很快就会无法管理.

谢谢

Hea*_*her 10

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

而是在不同目录中的部分文件中进行编辑.要添加属性,请在分部类定义的顶部声明元数据类.

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

现在,在Metadata类中,您可以定义属性或其他逻辑:

public class BlahMetadata
{
    [AllowHtml] 
    public string Content{ get; set; } 
}
Run Code Online (Sandbox Code Playgroud)