Search code examples
entity-frameworkreferenceentitiesdbcontext

How to create reference to Context object in Entity Framework and how to work with DBContext objects?


I have a project that I am working on. I decided to use Entity Framework with DBContext generator (to have persistance unaware objects) and I am now stuck with some issues.

Here is how I set my solution:

Solution:
- MyProject (my web app project)
- BusinessObjects (project) contains myproject.tt file with all objects (entities) inside. Each one in separate .cs file.
- DataAccess (project) contains myproject.edmx, myproject.Context.tt files

Here is my question: (I am new to all of this)

Let's say I have object (entity) Job and I want to define select, insert, update and delete methods on this object. Where do I do that? I tried to create folder Custom (where I would put all my customizations) inside BusinessObjects project. Then, I wanted to define my methods there but I don't know how to create new instance of ctx (context) object.

Here is my code:

namespace BusinessObjects
{
    public partial class Job
    {
        public Job GetJob(Guid Id) {
            using (var ctx = new BestGigEntities())

            }
            return null; //for now
        }
    }
}

The error message I get is that BestGigEntities does not exist in namespace. BestGigEntities should live in BusinessObjects, but somehow it is not visible when I tried to access it from withing BusinessObject project. But I can see it from my main web project. In myproject.Context.tt I have BusinessObjects specified as Custom Tool Namespace. Why I can't see it?

I have checked my myproject.Context.cs file and I can see

    public partial class BestGigEntities : DbContext
    {
        public BestGigEntities()
            : base("name=BestGigEntities")
        {
 . ...

Everything seems to be ok. I am almost sure I added all references properly. I am thinking maybe I am trying to define these methods on wrong place?

BestGigEntities is visible from my web project and I can use it from there.

Any help is appreciated.


Solution

  • I wouldn't recommend You to extend partial classes of your model. It would be better if You got acquainted with MVVM pattern and used it.
    Here are described some classes like services and helpers. Possibly they could prompt You where to begin to get the project more structured.
    As for CRUD opeations in the simplest case it'll look like

    public static Job Get(int jobId)
    {
        using (var context = new BestGigEntities())
        {
            return context.Jobs.FirstOrDefault(s => s.Id == jobId);
        }
    }
    
    public static void Save(Job job)
    {
        using (var context = new BestGigEntities())
        {
            context.Jobs.Attach(job);
            context.Entry(job).State = EntityState.Modified;
            context.SaveChanges();
        }
    }
    
    public static void Create(Job job)
    {
        using (var context = new BestGigEntities())
        {
            context.Jobs.Add(job);
            context.SaveChanges();
        }
    }
    
    public static void Delete(Job job)
    {
        using (var context = new BestGigEntities())
        {
            context.Entry(job).State = EntityState.Deleted;
            context.SaveChanges();
        }
    }