0

I'm using ASP.NET MVC 3.0 Database First approach with EF 4.x. Models are generated with EF 4.x DBContext Generator. Now I want to change model structure but don't want to change existing model because if database is modified I have to generate model again so the changes will be gone. I think it should be done by overriding partial classes but not clear. Is there any example to do it? what is the best way to implement it?

Edit: I have to add some additional validations in some Models, not all. I want to create independent models which overrides the generated classes and in case if EF models are regenerated there is no harm on our customized models.

Brij
  • 6,086
  • 9
  • 41
  • 69
  • if I undrestand you correctly you want to update certain model class but you do not want it to be erased when you update the whole model – COLD TOLD Apr 12 '12 at 06:01
  • when you update model update only classes/tables that you think will change – COLD TOLD Apr 12 '12 at 07:29

1 Answers1

1
  1. you can change the *.tt file for generating objects from model. What do you want to change objects.
  2. You can create partial classes like this

    namespace MyProject.Data{ public partial class User{} }

but your partial class should be the same namespace of EF created classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

    [MetadataType(typeof(UserMetadata))]
    public partial class User : IEntity
    {
        internal class UserMetadata
        {
            [DisplayName("User Name")]
            public virtual string UserName
            {
                get;
                set;
            }
            [DisplayName("First Name")]
            public virtual string FirstName
            {
                get;
                set;
            }

            [DisplayName("LastName")]
            public virtual string LastName
            {
                get;
                set;
            }
        }
    }
Yorgo
  • 2,668
  • 1
  • 16
  • 24