Cascading problems

I am finding cascades very confusing. Invariably I wind up with an error from the ORM telling me I have circular/multiple cascades paths.

I’ll tell you, at this point, I am VERY tempted to drop any and all use of an orm. Is there a better solution to all this that doesn’t involve the coding overhead of raw ado.net?

Bah…here’s what I resorted to: using the EF orm, just without the ‘R’ part. In other words, the ‘entities’ really are one dimensional row storage units.

It worked out very nice actually. Here’s the code:

namespace Blog.Domain
{
    public class Category
    {
        public int Id { get; protected set; }
        public string Name { get; set; }
    }
}
using System.Data.Entity;
using Blog.Domain;
namespace Blog.Persistence
{
    internal class Session : DbContext
    {
        public DbSet<Category> Categories { get; set; }
    }
}
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Blog.Domain.Persistence;
namespace Blog.Persistence
{
    public class Repository<T> : IRepository<T>
        where T : class
    {
        public IEnumerable<T> Select()
        {
            using (var session = new Session())
                return session.Set<T>().ToArray().AsEnumerable();
        }
        public void Insert(T entity)
        {
            using (var session = new Session())
            {
                session.Entry(entity).State = EntityState.Added;
                session.SaveChanges();
            }
        }
        public void Update(T entity)
        {
            using (var session = new Session())
            {
                session.Entry(entity).State = EntityState.Modified;
                session.SaveChanges();
            }
        }
        public void Delete(T entity)
        {
            using (var session = new Session())
            {
                session.Entry(entity).State = EntityState.Deleted;
                session.SaveChanges();
            }
        }
    }
}
using System.Linq;
using Blog.Domain;
using Blog.Domain.Persistence;
namespace Blog.Persistence
{
    public class CategoryRepository : Repository<Category>, ICategoryRepository
    {
        public Category SelectById(int id)
        {
            using (var session = new Session())
                return session.Set<Category>().SingleOrDefault(x => x.Id == id);
        }
    
    }
}

Now I don’t have to worry about dumb collections, eager vs lazy loading, cross-context errors, and still retain the nice mapping ability of the orm, such as testing nullable field values before assignment, and the inclusion of linq.