Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:19:18+00:00 2026-05-28T19:19:18+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:19:20+00:00Added an answer on May 28, 2026 at 7:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the process of building my first web application using ASP.NET MVC 2
I am building a web application using ASP.NET MVC that has two very distinct
I'm am building my asp.net web application using MVC (Preview 5), and am also
My team is considering building our next web app using the ASP.NET MVC framework.
I am building an ASP.NET MVC application using the 1.0 release using Visual Web
I am building an ASP.NET MVC web application and I'm using jQuery for some
I'm building a web app using EF Code First and ASP.NET MVC. I have
I've started building a ASP net MVC 2.0 web application using SQL Express 2008
I'm building a small web application with ASP.NET MVC 2, using db4o as a
I am currently building a very small/simple web application in ASP.NET MVC with ADO.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.