Using EF POCO Classes as MVC 2 Models (with Data Annotations)

I have a 4-layer web application programmed in C # .... Net 4.0:

  • User interface level
  • Business level
  • Data access level
  • Entity level

My data layer contains edmx The level of my objects contains my POCO objects (generated using the t4 script), and this level is referenced in all other layers.

When creating an MVC form to create a new client, for example .... I already have a client class with fields for first name, last name, etc. at the level of my entities, but this automatically generated POCO class does not have data annotations for verification ... IE [Required], etc. To submit a form

My solution right now is to create new model classes that pretty much match my poco classes, but also contain these additional annotations for validation.

What I want to know is an easy way to use certain POCO objects in the MVC model (at the user interface level) without having to rewrite the class ... and also without changing the t4 that generates these POCO classes (since I'm not up to speed t4).

I saw this from another post in stackoverflow http://automapper.codeplex.com/ ... not sure if this will do this or be the best solution.

+3
source share
2 answers

T4 . T4, , , ( NotifyPropertyChange .., POCO MVC UI Silverlight).

, T4, , .

+4

POCO :

public class Person {
    public string FirstName { get; set; }
    public string LastName  { get; set; }
}

, T4, , :

[MetadataType(typeof(PersonMetadata))]
public partial class Person {

    internal class PersonMetadata {

        [Required]
        // insert other metadata here
        public string FirstName { get; set; }

        // and if you don't want metadata for lastname, you can leave it out
    }
}

- , , , . , , , ( )

+6

Source: https://habr.com/ru/post/1748586/


All Articles