数据库第一的方针和修改数据库架构 [英] Database-first approach and modifying the database schema

查看:188
本文介绍了数据库第一的方针和修改数据库架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用实体框架的DbContext使用数据库第一种方法的ASP.NET MVC Web应用程序。

如果在某些情况下,我需要修改数据库如添加新表或修改现有的表(​​添加列或更改列数据类型),我应该:


  1. 删除现有的.edmx实体和.TT文件夹并重新创建重新映射

  2. 手动修改适用于.TT文件夹下的模型类修改数据库后

  3. 无论这两个选项都有效。

我应该如何接近修改我的数据库架构的任务?

编辑
比如我有一个是自动的.TT文件夹下创建以下部分类,因为我已经添加了 *的 IsManagedBy 的* 帮手methodand的[MetadataType(typeof运算霸菱记(Books_validation))到它: -

  [MetadataType(typeof运算(Books_validation))]
    公共部分类图书
    {
        公共图书()
        {
            this.Assessments =新的HashSet<评估及GT;();
            this.Users_Books =新的HashSet< Users_Books>();
        }        公众诠释的BookID {搞定;组; }
        公共字符串BOOKNAME {搞定;组; }
        公共字符串的ManagedBy {搞定;组; }        公共字节[] {时间戳获得;组; }        公共虚拟的ICollection<评估和GT;评估{搞定;组; }
        公共虚拟用户用户{搞定;组; }
        公共虚拟的ICollection< Users_Books> Users_Bookes {搞定;组; }
        公共BOOL IsManagedBy(用户名字符串)
        {
            返回ManagedBy.Equals(用户名,
            StringComparison.OrdinalIgnoreCase);
        }    }

然后我创建Book_validation类应用数据注解如下: -

 公共类Books_validation    {        [必需(的ErrorMessage =名称是必需)]
        公共字符串BOOKNAME {搞定;组; }
        公共字符串的ManagedBy {搞定;组; }
       [ConcurrencyCheck]
        [时间戳]
       公共字节[] {时间戳获得;组; }    }

此方法导致三个问题: -

1 的IsManagedBy辅助方法,不能在Book_validation类中定义的,我应该在自动生成的图书部分类,这意味着如果我重新生成code将被删除其定义!

2 例如,如果我修改这是关系到Book类(外键),然后我选择了更新模型从数据库,从设计师的.edmx选项评估表。然后视觉工作室也将重新生成Book类,这将导致更多的麻烦我。

3 均匀的[MetadataType(typeof运算(Books_validation))]我在自动生成的code写也将在盒中取出我重新生成code,所以这也就是说,我必须去对所有的修饰类,并添加相关MetadataType(typeof运算)他们。

当我使用的ObjectContext的模板,因为模板ObjectContext的也不会自动生成部分类,它只会生成模型类,然后我可以添加部分类,并为他们的metadatetype

所有这些问题都没有发生......所以我认为这是更好地返回到ObjectContext的,而不是的DbContext !!!,任何建议就这个问题和上面的问题呢?
BR


解决方案

如果您在EDMX右键单击编辑器,应该有一个选项从数据库更新模型。如果您在数据库中取出的字段,你可能仍然需要将其删除,甚至删除其表,并通过更新模型从数据库菜单中重新添加它,但我觉得这不是删除整个EDMX并启动更容易新鲜的各一次。

更新

由于拉吉斯拉夫说,你永远不应该乱用自动生成的code文件。正如你已经发现,那些code文件被自动地改写。实体框架的创建者预见到了您遇到的问题,他们提供了一个机制,让您可以添加到这些类,而无需修改自动生成的.cs文件:部分类

部分类允许您创建跨越多个文件类的定义,只要它们都在同一个命名空间和装配。因此除了自动生成的文件Context.cs,您可以创建附加文件是这样的:


  

Book.cs


  [MetadataType(typeof运算(Books_validation))]
公共部分类图书
{
    公共BOOL IsManagedBy(用户名字符串)
    {
        返回ManagedBy.Equals(用户名,
        StringComparison.OrdinalIgnoreCase);
    }
}

部分类的这个单独定义的presence将导致此code是有效的合并的自动生成的书上课的时候​​汇编的编译,但它可以让你有这种特殊定制code在一个单独的文件,当您从EDMX文件重新生成模型数据,将不会受到影响。

I am building an ASP.NET MVC web application using entity framework DbContext using the database first approach.

If under certain conditions I needed to modify the database such as adding new table or modifying an existing table (adding columns or changing a column data type), should I:

  1. Remove the existing entity .edmx and .tt folders and re-create the mapping again
  2. Manually apply the modification to the model classes under the .tt folder after modifying the database
  3. Neither of these two options are valid.

How should I approach the task of modifying my database schema?

Edited For example i have the following partial class which were automatically created under the .tt folder, baring in mind that i have added the *IsManagedBy* helper methodand the [MetadataType(typeof(Books_validation))] to it :-

[MetadataType(typeof(Books_validation))]
    public partial class Book
    {
        public Book()
        {
            this.Assessments = new HashSet<Assessment>();
            this.Users_Books = new HashSet<Users_Books>();
        }

        public int BookID { get; set; }


        public string BookName { get; set; }
        public string ManagedBy { get; set; }

        public byte[] Timestamp { get; set; }

        public virtual ICollection<Assessment> Assessments { get; set; }
        public virtual User User { get; set; }
        public virtual ICollection<Users_Books> Users_Bookes { get; set; }
        public bool IsManagedBy(string userName)
        {
            return ManagedBy.Equals(userName,
            StringComparison.OrdinalIgnoreCase);
        }

    }

And then i create the Book_validation class to apply the data annotations as following:-

public class Books_validation

    {

        [Required(ErrorMessage = "Name is required")]
        public string BookName { get; set; }
        public string ManagedBy { get; set; }
       [ConcurrencyCheck]
        [Timestamp]
       public Byte[] Timestamp { get; set; }

    }

This approach is causing three problems:-

1. The IsManagedBy helper method cannot be defined in the Book_validation class and i should define it in the automatically generated Book partial class , which means it will be removed if i regenerate the code !!!.

2. If for example i modify the Assessment table which is related to the Book class (by Foreign key ) and then i chose the "Update Model from Database" option from the .edmx designer; then visual studio will also regenerate the Book class , which will cause more troubles for me.

3. Even the [MetadataType(typeof(Books_validation))] which i wrote in the automatically generated code will also be removed in case i regenerate the code, so this means that i have to go over all the modified classes and add the associated MetadataType(typeof) for them.

All these problems were not happening when i was using the ObjectContext template, since the ObjectContext template will not automatically generate partial classes , it will only generate model classes and then i can add partial classes and the metadatetype for them... so i think it is better to return back to ObjectContext instead of DBContext !!!, any suggestion on this and the above problems? BR

解决方案

If you right-click in the EDMX editor, there should be an option to "Update Model from Database." If you have removed fields in the database, you may still need to remove them, or even remove their table and re-add it via the "Update Model from Database" menu, but I find this much easier than deleting the whole edmx and starting fresh each time.

Update

As Ladislav says, you should never mess with auto-generated code files. As you have discovered, those code files get rewritten automatically. The creators of Entity Framework foresaw the issue you're having, and they provided a mechanism whereby you can add to those classes without modifying the auto-generated .cs file: partial classes.

Partial classes allow you to create class definitions that span multiple files, provided they are all in the same namespace and assembly. So alongside the autogenerated Context.cs file, you can create additional files like this:

Book.cs

[MetadataType(typeof(Books_validation))]
public partial class Book
{
    public bool IsManagedBy(string userName)
    {
        return ManagedBy.Equals(userName,
        StringComparison.OrdinalIgnoreCase);
    }
}

The presence of this separate definition of the partial class will cause this code to be effectively "merged" with the auto-generated Book class when the assembly is compiled, but it allows you to have this special custom code in a separate file which won't be affected when you regenerate the model data from the EDMX file.

这篇关于数据库第一的方针和修改数据库架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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