none
POCO EntitySet Uses Wrong Table Name RRS feed

  • Question

  • I am using Poco and the objectContext to access data entities and it seems that EF 4.1 is appending the word "Set" to the EntitySet name. I have a table and class named "Category" but when I try a simple query it fails because it cannot find the table "CategorySet" in the DB. I tried using the data annotation below to  try to correct this but no luck.

        [Table("Category")]
        public class Category




    • Edited by johnsql Sunday, June 3, 2012 12:44 AM
    Sunday, June 3, 2012 12:16 AM

All replies

  • I found the answer to this problem is to use RegisterSet to define the EntitySet when building the context. I still would like to know why that DataAnnotation did not work.

                builder.RegisterSet<Category>("Category");

    Also, here's the blog that lead me to solving this. 

    http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/

    • Marked as answer by johnsql Sunday, June 3, 2012 12:46 AM
    • Edited by johnsql Sunday, June 3, 2012 12:48 AM
    • Unmarked as answer by johnsql Tuesday, June 5, 2012 4:28 AM
    Sunday, June 3, 2012 12:46 AM
  • Hi johnsql,

    Welcome to MSDN Forum.

    I'm glad to hear that you have solved the issue, and thanks for your sharing.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    Monday, June 4, 2012 2:27 AM
    Moderator
  • I am reopening this one as I am having the same exact problem but this time it is when I am querying/joining across multiple tables. Using the RegisterSet property to set the table name works when I am hitting one table only, but if I have to join to multiple tables, EF then starts appending the "Set" to each table name. I am using N-tier Poco and the Repository pattern to query the entities and as you can see I am do set the table name in multiple places, however, when I am getting the objectContext I am only setting it to the table I am trying to retrieve, this works except when multiple tables as in the GetBySubCategory() I am getting invalid object "SubCategorySet" could not be found. Am I supposed to RegisterSet for each table in the query? If so how do you create multiple objectContexts for each table and still tie it all together? Getting really frustrated here trying to use EF in N-tier without the dependencies of T4 and EDMX

    public class SubCategoryRepository { private IEnumerable<SubCategory> _subCategories; public IEnumerable<SubCategory> SubCategories { get { return _subCategories; } } public Repository<SubCategory> _subCategoryRepository; public SubCategoryRepository() { ContextBuilderRepository<SubCategory, SubCategoryConfiguration> contextBuilder = new ContextBuilderRepository<SubCategory, SubCategoryConfiguration>("SubCategory"); var contextAdapter = new ObjectContextAdapter(contextBuilder.context); var unitOfWork = new UnitOfWork(contextAdapter); _subCategoryRepository = new Repository<SubCategory>(contextAdapter); } public IEnumerable<SubCategory> GetAll() { _subCategories = _subCategoryRepository.GetAll(); return _subCategories; } public IEnumerable<SubCategory> GetByCategoryName(string name) { return _subCategoryRepository.Find(x => x.Category.CategoryName == name); } } [Table("Category")] public class Category { public Category() { } public int CategoryId { get; set; } public string CategoryName { get; set; } public DateTime DateCreated { get; set; } public ICollection<SubCategory> SubCategories { get; set; } } } [Table("SubCategory")] public class SubCategory { public int SubCategoryId { get; set; } public string SubCategoryName { get; set; } public virtual int CategoryId { get; set; } [ForeignKey("CategoryId")] public virtual Category Category { get; set; } public DateTime DateCreated { get; set; } }

        public class SubCategoryConfiguration : EntityConfiguration<SubCategory>
        {
            public SubCategoryConfiguration()
            {
                Property(s => s.SubCategoryId).IsIdentity();
                Property(s => s.SubCategoryName).IsRequired();
                Relationship(s => s.Category).FromProperty(c => c.SubCategories);
            }
        }

        public class Repository<T> : IRepository<T> where T : class
        {
            IObjectSet<T> _objectSet;

            public Repository(IObjectContext objectContext)
            {
                _objectSet = objectContext.CreateObjectSet<T>();
            }
            public IQueryable<T> AsQueryable()
            {
                return _objectSet;
            }
            public IEnumerable<T> GetAll()
            {
                return _objectSet.ToList();
            }
            public IEnumerable<T> Find(Expression<Func<T, bool>> where)
            {
                return _objectSet.Where(where);
            }

        }

        public class ObjectContextAdapter : IObjectContext
        {
            readonly ObjectContext _context;

            public ObjectContextAdapter(ObjectContext context)
            {
                _context = context;
            }

            public void Dispose()
            {
                _context.Dispose();
            }

            public IObjectSet<T> CreateObjectSet<T>() where T : class
            {
                return _context.CreateObjectSet<T>();
            }

            public void SaveChanges()
            {
                _context.SaveChanges();
            }
        }

        public class UnitOfWork : IUnitOfWork, IDisposable
        {
            private readonly IObjectContext _objectContext;

            public UnitOfWork(IObjectContext objectContext)
            {
                _objectContext = objectContext;
            }

            public void Dispose()
            {
                if (_objectContext != null)
                {
                    _objectContext.Dispose();
                }
                GC.SuppressFinalize(this);
            }

            public void Commit()
            {
                //_objectContext.SaveChanges();
            }
        }

        public class ContextBuilderRepository<T, C> where T : class where C : StructuralTypeConfiguration, new()
        {
            public ObjectContext context { get; set; }
            public static SqlConnection connection = new SqlConnection("");
            public ContextBuilderRepository(string tableName)
            {
                var builder = new ContextBuilder<ObjectContext>(); 
                builder.Configurations.Add(new C());
                builder.RegisterSet<T>(tableName);
                context = builder.Create(connection);
            }
        }


    Tuesday, June 5, 2012 4:42 AM