1

So I am new to MVC and am working now with MVC3 and the Entity Framework. I am currently using a Model/Schema 1st approach as the database already exists. Therefore I create my ADO.NET Entity Data Model (.edmx) and I have all the needed entities in my MVC app. So far so good on all this.

However let's say one of my Entities is a 'Customer' entity. I can see the auto-generated partial class inheriting from EntityObject in MyEntites.Designer.cs. At this point I want to add some custom business logic to my MVC Model. Natuarally I believe the answer is to use my own partial class created in the Model named 'Customer' as well.

I did a lot of searching on this before asking the question and see all kinds of information on POCO, T4 templates, modifying auto-generated code, etc and am lost. Is it a royal pain to add my own business logic and custom code to the auto-generated entities from EF? I certainly don't want to modify the auto generated code over and over.

I need a straight forward explanation to make the proverbial 'light bulb' go on, and then I can take-off from there. Can someone help me answer how to do this please?

Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
atconway
  • 20,624
  • 30
  • 159
  • 229

2 Answers2

2

Keep your own class code in a different file, but use the same class and namespace. This will help avoid your code being overwritten by the T4 code generator.

Extending Entity Framework Generated Types

You can also add attributes to generated classes by using a meta class:

Adding Attributes to Generated Classes

MCattle
  • 2,897
  • 2
  • 38
  • 54
  • Thank you, but I am already as far in my understanding as your answer provided. With the exception of the T4 templates which I stated I did not understand how it related to the overall solution (or is the solution). – atconway May 10 '12 at 14:37
  • I've expanded my answer to include custom attributes, but having your own partial class to extend the generated class should do everything you need, unless I'm misunderstanding the question. There's no need to get into T4 templates unless you don't like the code that's generated and wish to change it. – MCattle May 10 '12 at 14:45
  • 1
    I may be a newbie to MVC (not to SE though), but I disagree with *"Beyond this, business logic can be contained in the Controller layer of MVC."* Everything I read (and agree w/ so far) says make the Model fat (incl. BL) and the Controllers lean (no BL). http://stackoverflow.com/questions/235233/asp-net-mvc-should-business-logic-exist-in-controllers – atconway May 10 '12 at 14:58
2

Those codes are auto-generated and will be over written on each model update or change.

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 string Description { 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