Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

1) I am using the Database first approach in my project, below is a small sample to describe what i want to achieve in my actual project.

2) I have generated an entity model from my database which generates the following code:

C#
namespace Test
{
    using System;
    using System.Collections.Generic;

    public partial class Student
    {
        public Student()
        {
            this.Enrollments = new HashSet<Enrollment>();
        }

        public int StudentID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public System.DateTime EnrollmentDate { get; set; }

        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }
}


3) I made some changes in the above given Student class, i added an attribute and a property:

C#
using System.ComponentModel.DataAnnotations;

[Required()]
public int StudentID { get; set; }

public string FullName { get; set; }


4) Then i updated the model from database "Update Model from Database", and lost all the changes which i made in the Student class.

I want to know if there is any way to preserve these changes after updating the model from database?

Any and all help will be highly appreciated.

Thanks
Posted

Hey Phil,

I was going through articles online and found a concept called “Buddy” classes. Here is the code I just wrote but not tested till yet:

New Class:

C#
namespace Test
{
    public class StudentMetaData
    {

        [Required()]
        public int StudentID { get; set; }

        public string FullName { get; set; }

    }
}



Modified generated class:

C#
namespace Test
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    [MetadataType(typeof(StudentMetaData))]
    public partial class Student
    {
        public Student()
        {
            this.Enrollments = new HashSet<Enrollment>();
        }

        public int StudentID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public System.DateTime EnrollmentDate { get; set; }

        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }
}


thanks
 
Share this answer
 
Comments
phil.o 7-Mar-14 19:11pm    
Thanks for sharing what you found :) Indeed your solution is the way to go, so I vote it a 5. You may also mark your question as answered.
Good luck.
[no name] 7-Mar-14 23:18pm    
Thanks a lot Phil...
As this class is declared as partial, you have the ability to create your own file, in which you can declare the same partial class, with your own code.
The code generator will always replace the code in the original file, there's no way to skip it; on the contrary, it will never touch anything in your custom own file.
You just have to match namespace and class name.

Hope this helps.
 
Share this answer
 
Comments
[no name] 7-Mar-14 15:49pm    
Hey Phil,

Thanks for your reply, do you mean something like this:

Code generator:

namespace Test
{
using System;
using System.Collections.Generic;

public partial class Student
{
public Student()
{
this.Enrollments = new HashSet&lt;Enrollment&gt;();
}

public int StudentID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public System.DateTime EnrollmentDate { get; set; }

public virtual ICollection&lt;Enrollment&gt; Enrollments { get; set; }
}
}

My Code:

namespace Test
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;


public partial class Student
{
[Required()]
public int StudentID { get; set; }

public string FullName { get; set; }

}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900