Partial class on a model when Entity Framework is in separate library

asp.net-mvc entity-framework partial-classes

Question

I am not sure if this is the correct way to go, if so, please advise otherwise.

This is an ASP.Net MVC 4 site using EF 5.x

Suppose you have your Entity Framework in a class library on it's own. Code Generation Item has now generated all of your Models (the xxx.tt section of your EF mode)

This project is then added/referenced in the development of a site.

You can now access the data via the EF.

Now - in the site project I want to create a partial class of one of my EF models, for example "Users", with an additional property that isn't in the DB.

In the past on a web forms project when the EF was part of the project and not a reference I would simply create the partial class and all would be good; my "Users" would now have a bunch of other stuff in it that wasn't database related but needed on the "User".

I can't seem to get this to work in this MVC project where the EF is in a separate project.

I have tried doing this for example:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyTestEntity.Entity;

namespace MyTestMVCSite.Models
{
    public partial class Email
    {       
        public string OtherEmail {
            get { return "me@myEmail.com"; }
        }       
    }
}

I have also tried inheriting the EF models class, like this:

public partial class Email : MyTestEntity.Entity.Email
{       
    public string OtherEmail {
        get { return "me@myEmail.com"; }
    }       
}

Nothing I seem to be doing gives me access to "OtherEmail"

What I actually want to be able to do is create a partial class for some of my models and then have this partial class implement an interface so i can inject an instance of this interface into another object rather than overloading.

Am i talking crazy nonsense?

1
2
8/30/2013 3:45:08 PM

Accepted Answer

If you wish to augment your models with additional properties used for display purposes, then you should consider using view models, and a mechanism for mapping data to and from your models to view models.

You can then perform validation independently from the model based on the current view. View models will also protect you from accidentally exposing properties on your model that you do not wish users to alter through post data, even if you haven't explicitly specified them in your view.

3
8/30/2013 3:50:03 PM

Popular Answer

You cannot have two partial classes referring to the same class in two different assemblies (projects). Once the assembly is compiled, the meta-data is baked in, and your classes are no longer partial. Partial classes allows you to split the definition of the same class into two files.

Is it possible to have two partial classes in different assemblies represent the same class?



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow