Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9096209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:47:18+00:00 2026-06-16T23:47:18+00:00

I’m trying to implement the Repository/Unit-of-Work pattern for EF5 on .NET 4.5 as described

  • 0

I’m trying to implement the Repository/Unit-of-Work pattern for EF5 on .NET 4.5 as described here: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application. This all works great until I try to use self-referencing entities as described here: http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code. When I try to do anything (get a specify entity, get all entities, add an entity, etc.) with the repository that contains the self referencing entity (FieldType) I get a stackoverflowexception on the getter of the FieldTypeRepository in my UnitOfWork class. Don’t worry, the irony of posting a stackoverflowexception on stackoverflow isn’t lost on me. I’ve read quite a few SO articles about the repository pattern for EF but haven’t seen anything combining them with self-referencing entities.

Things I’ve tried:

  • Using FluentAPI instead of attributes on the FieldType entity – no change
  • Using the Context object directly – this works perfectly, entities are added to the DB and reference each other properly, unfortunately that defeats the purpose of the repository pattern
  • Using the repository for non-self referencing entities – works great
  • Using EF5 for .NET 4.0 and .NET 4.5 – same results

My code:

FieldType.cs

public class FieldType
{
    [Key]
    [DatabaseGeneratedAttribute(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    public int FieldTypeId { get; private set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public int? ParentFieldTypeId { get; set; }

    [ForeignKey("ParentFieldTypeId")]
    public virtual FieldType ParentFieldType { get; set; }
}

UnitOfWork.cs

public class UnitOfWork : IDisposable
{
    private Context context = new Context();

    private Repository<FieldType> fieldTypeRepository;

    public Repository<FieldType> FieldTypeRepository
    {
        // EXCEPTION OCCURS HERE
        get
        {
            if (this.fieldTypeRepository == null)
            {
                this.fieldTypeRepository = new Repository<FieldType>(this.context);
            }

            return this.FieldTypeRepository;
        }
    }

    public void Save()
    {
        this.context.SaveChanges();
    }
}

Context.cs

public class Context : DbContext
{
    public Context()
        : base("echo")
    {
    }

    public DbSet<FieldType> FieldTypes { get; set; }
}

Repository.cs

public class Repository<TEntity> where TEntity : class
{
    private Context context;

    private DbSet<TEntity> dbSet;

    public Repository(Context context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includedProperties = "")
    {
        IQueryable<TEntity> query = this.dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includedProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

    public virtual TEntity GetById(object id)
    {
        return this.dbSet.Find(id);
    }

    public virtual void Insert(TEntity entity)
    {
        this.dbSet.Add(entity);
    }

    public virtual void Delete(object id)
    {
        TEntity entityToDelete = this.dbSet.Find(id);
        this.Delete(entityToDelete);
    }

    public virtual void Delete(TEntity entityToDelete)
    {
        if (this.context.Entry(entityToDelete).State == EntityState.Detached)
        {
            this.dbSet.Attach(entityToDelete);
        }

        this.dbSet.Remove(entityToDelete);
    }

    public virtual void Update(TEntity entityToUpdate)
    {
        this.dbSet.Attach(entityToUpdate);
        this.context.Entry(entityToUpdate).State = EntityState.Modified;
    }
}

SecurityController.cs (or any class really)

public class SecurityController : Controller
{
    public ActionResult Login()
    {
        using (UnitOfWork unitOfWork = new UnitOfWork())
        {
            var fieldType = unitOfWork.FieldTypeRepository.GetById(1);
            //THIS COULD BE ANY TYPE OPERATION
        }

        return this.View();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:47:19+00:00Added an answer on June 16, 2026 at 11:47 pm

    Thanks Gert, for helping me realize that it’s not a problem with EF or self-referencing types but rather that I was calling the get method for the FieldTypeRepository from itself… http://www.youtube.com/watch?v=g6GuEswXOXo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.