1

I am working on an asp.net mvc-5 web application, and i am using entity framework 5.0. I have mapped my SQL server database tables which created an .edmx file. now i want to extend the model classes to have additional non-database attributes, so i created a partial class for my model class and i provided an additional attribute as follow:-

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Test.Models
{

    public partial class User
    {
         [NotMapped]
         public string NonDataBaseColumn { set; get; }
    }
}

so i have these questions:

  1. is this a valid approach to pass additional attributes , by defining additional columns inside the partial classes.
  2. since i am using database first approach, so do i need to add [NotMapped] data annotation, or the [NotMapped] is only valid when following code-first approach ?
  3. could under any situation the NotMapped column (NonDataBaseColumn in my case) get created inside the Databse automatically ?
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
John John
  • 1
  • 72
  • 238
  • 501
  • @CodeCaster thanks for the reply. but why i sohlud not use EF models as MVC viewmodels ? is this due to security issues ? and as you said this is common practice in most tutorial and mvc books ... now my questions are the 3 questions i provided .. could you reply to them .. thanks.. espiacally what will happen if i do not add the [NotMapped] attribute ,, will this cause the column to be created inside the DB ? – John John Jan 05 '16 at 13:14
  • @CodeCaster yes i totally agree with you.. but can you please advice on this "what will happen if i do not add the [NotMapped] attribute ,, will this cause the column to be created inside the DB ? " ? – John John Jan 05 '16 at 13:20
  • "but why i sohlud not use EF models as MVC viewmodels" Because what do you do if you want to present your EF model in a different way? Do you know have two sets of [NotMapped] properties and only use half? – Thomas Boby Jan 05 '16 at 14:29

1 Answers1

11

is this a valid approach to pass additional attributes, by defining additional columns inside the partial classes?

Technically it is, but repeat after me: don't use Entity Framework models as MVC viewmodels, and spread the word.

I still don't get why virtually every tutorial displays this horrible practice which spawns twenty-odd questions like this one a day, but it is not the way it should be done. Domain models are not view models.

Those tutorials probably do that because it works perfectly fine for a "My first TODO WebApp", so they have less code to show and less good development practices to explain, leaving you with the mess when it breaks horribly for anything more complex than that.

For example, try to add a SelectList property to an entity to provide a dropdown with data, then extract your data layer into a DAL library, and be left scratching your head wondering why you have to reference MVC from your DAL.

See also: ASP.NET MVC: using EF entities as viewmodels?

As for your questions on the [NotMapped] attribute: it does exactly what its name and documentation state, it causes the property it's applied to not to be mapped to a database column.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • ok thanks , but let say i did not specify the [NotMapped] what will this cause ? compared to having it ? will my application force the column to be created inside the DB ? or the [NotMapped] is mainly used to ignore the column when querying the DB ? but having it or not having it will not cause the column to be automatically created insidethe DB ? – John John Jan 05 '16 at 13:24
  • 1
    Using EDMX and Database First you don't have to apply `[NotMapped]`. Using an EDMX your mapping is specified in the EDMX. – CodeCaster Jan 05 '16 at 13:25
  • so in my case with .edmx & DB first approach i do not have to specify [NotMapped] is this correct ? as the column will be ignored when querying the DB in all cases? second question but if i am following code-first approach then not specifying [NotMapped] will cause the column to be created inside the DB is this correct ? – John John Jan 05 '16 at 13:28
  • How about you go read an Entity Framework tutorial, or rather, the documentation, [or searching](http://stackoverflow.com/questions/10385248/ignoring-a-class-property-in-entity-framework-4-1-code-first)? :) Using Code First, all properties on your entity classes will be mapped to database columns, unless marked as `[NotMapped]`. – CodeCaster Jan 05 '16 at 13:29
  • Your opinion is not fact. Creating a ton of pointless view models and mapping the data (or even worse using the travesty that is automapper) causes as many problems from the opposite direction. More often than not, blindly creating tons of pointless MVC viewmodels that are simple copies of the underlying domain model is simply bad programming and violates DRY, YAGNI and KISS. – mattmanser Oct 28 '16 at 12:46
  • Because while somewhere in your rant there's an answer, it's more rant than answer. Of your 6 paragraphs, only the last one is part of the answer. – mattmanser Oct 28 '16 at 16:14
  • @CodeCaster Technically An EF Entity is not a domain Entity ;) – Vlad Teodor Jan 14 '17 at 07:26
  • Adding the "[NotMapped] public ListAllWhatevers" property to my entity to support a – Erik Midtskogen Feb 24 '22 at 14:18