Database-first approach and modifying the database

2019-07-18 17:14发布

问题:

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

回答1:

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.



回答2:

You should never modify auto generated code. If you do it you have spoiled the whole workflow anyway because any single change to EDMX file can delete your changes. All custom code belongs to your own partial part of the class (and it is for example used for adding validation data annotations).

Once you follow correct workflow you will be able to modify EDMX and use Update from database which is the only correct way to push changes from database into your code.