3

I am starting MVC4 with VS2012. I am also using EF5 with the "Database First" method of creating my classes.

However because the generated glasses can be regenerated I cannot put the Data Annotation details to assist with validation.

I have seen some code snippets that use MetaData and partial classes but I was wondering if anyone knows of a small compilable example that I can look at and pull apart to better understand how the vasious classes interlink.

Many many thanks for any help. Dave

  • What kind of validation you would like to perform and what kind of validation mechanism (custom validation attribute, IValidatableObject etc) are you thinking about? I think you have a few options (buddy classes, partial classes, updating T4 templates) depending on what you want to validate and how you want to validate it. – Pawel Nov 05 '12 at 05:20
  • Hi Pawel. Sorry for not replying sooner but I did not get a email notification. Basically the type of validation I want to use is the standard [Required] etc data annotation validation so that the View can automatically use it. Many thanks for replying. Dave – David Boccabella Nov 09 '12 at 05:05
  • I think you need to look at buddy classes. Take a look here: http://msdn.microsoft.com/en-us/library/ee256141(v=VS.100).aspx, http://stackoverflow.com/questions/2999936/using-dataannotations-with-entity-framework and http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx (search for "buddy") – Pawel Nov 09 '12 at 19:27
  • Many many thanks for that Pawel. From the quick look (and trying to update the project to VS2012) it looks just what I need. I'll experiment some more later. – David Boccabella Nov 12 '12 at 01:53

1 Answers1

4

You can achieve what you need through extending models. Suppose that EF generated the following entity class for you:

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

    public partial class News
    {
        public int ID { get; set; }
        public string Title { get; set; }                  
        public int UserID { get; set; }

        public virtual UserProfile User{ get; set; }
    }
}

and you want do some work arounds to preserve your you data annotations and attributes. So, follow these steps:

First, add two classes some where (wherever you want, but it's better to be in Models) like the following:

namespace YourSolution
{
    [MetadataType(typeof(NewsAttribs))]
    public partial class News
    {
         // leave it empty.
    }

    public class NewsAttribs
    {            
        // Your attribs will come here.
    }
}

then add what properties and attributes you want to the second class - NewsAttribs here. :

public class NewsAttrib
{
    [Display(Name = "News title")]
    [Required(ErrorMessage = "Please enter the news title.")]
    public string Title { get; set; }

    // and other properties you want...
}

Notes:

1) The namespace of the generated entity class and your classes must be the same - here YourSolution.

2) your first class must be partial and its name must be the same as EF generated class.

Go through this and your attribs never been lost again ...

Amin Saqi
  • 18,549
  • 7
  • 50
  • 70
  • I was going to submit an edit but I can't since the edit wouldn't be at least 6 characters long. Anyway, in the last code example the class name should be NewsAttribs not NewsAttrib. – FirstDivision Nov 03 '15 at 23:32