10

I´ve not had much contact with Entity Framework yet and therefore I´d like to hear some guys with experience.

I´ve got an MVC project and my DataAccess lies in a different project where I want to place my EDMX file.

So how would I like to name this file? Per default it is "Model1.edmx" but in the context of MVC I don´t feel comfortable with this name. Is it really a Model? I tend to call it "DbModel" or something to indicate that it is database related stuff.

And how do you guys call the entity class? I think I like the EF typical name "DbContext".

So within my controllers I´d have something like

public class WorldController : Controller
{
    DbContext db = new DbContext();

    public ActionResult Own()
    {
        var allContinents = db.Continents;
        [...]
    }
}

Sorry for being fussy but I really do care about naming.

tereško
  • 58,060
  • 25
  • 98
  • 150
timmkrause
  • 3,367
  • 4
  • 32
  • 59

2 Answers2

7

It is good to care about naming!

How to do it depends on the composition your application. Let's say you have a web application for registering UFO sightings, to name something common. If there is a separate part of your db (maybe a separate schema) containing users, roles and permissions, you could create a context named AuthorizationContext, and for the business part a context named UfoDbContext.

I mean, if there are clear aggregates with no or little overlap you could create separate contexts for them with clear names. If one context fits the bill, I would still give it some meaningful name (not DbContext) that relates to your application domain.

There are people (I'm not one of them) that like to work with one context per application "column" (db to UI) or "user story". Meaningful names are even more important then.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • I need 2 EDMX files for 2 different schemas (on the same database) so I thinkg I will name them like this now: Schema1DbModel.edmx, Schema1DbContext and the same for Schema2. Would you agree with that? I guess it would more fit with our devs, because everyone knows the schemas very well and would be self-explanatory for us. – timmkrause Mar 26 '12 at 07:28
  • Or would you cut the "Db" in "DbModel" for the EDMX files? Use nothing instead or use "EfModel" like knoia said? – timmkrause Mar 26 '12 at 07:45
  • Whatever suits you, EfModel might be a good option, personally I like implementation-agnostic names. But whatever yo choose: be consistent! – Gert Arnold Mar 26 '12 at 10:30
  • Last question: What do you mean with implementation-agnostic? So you´d just pick "Model" (e.g. "Schema1Model")? – timmkrause Mar 26 '12 at 11:01
  • I mean not referring to e.g. entity framework. Tomorrow you may choose linq to sql (well, not likely, but you get the idea). – Gert Arnold Mar 26 '12 at 12:39
3

My suggestion would be to use something that indicates the contents in the naming. You could take part of your project name, for instance, and bring it down into the name of the EDMX. Include something that makes it less generic.

I also tend to include an "Ef" in my naming, which I started when working on a project that already had an existing ORM.

To take Microsoft's prototypical example: if your project fell under the umbrella name of Norwind, here's how I would name some of the files in my model project:

EDMX File: NorwindEfModel.edmx

Generator/TT File: NorwindEfDbContext.tt

Entities class: NorwindEntities

I can't say this is exactly how Microsoft would have it if you downloaded a sample project from them (though I believe it would be similar), but I think it's a reasonable naming structure and it fits my needs. The bottom line is this largely comes down to opinion and your need for specific distinction.

knoia
  • 165
  • 9