none
CodeFirst: Error when POCO inherited from same-named class RRS feed

  • Question

  • I have 2 projects for domain model's - called DomainModel and SuperDomainModel, for example.

    There are 2 same-named POCO's in this different projects (with different namespaces) - DomainModel.Product and SuperDomainModel.Product. POCO's have next properties sets:

    namespace SuperDomainModel
    {
        public class Product
        {
            public int SuperProperty { get; set; }
        }
    }
    
    namespace DomainModel
    {
        public class Product:SuperDomainModel.Product
        {
            public int ProductId { get; set; }
            public string Name { get; set; }
    
            public override string ToString()
            {
                return String.Format("{0}", Name);
            }
        }
    }
    
    Both projects referenced to EntityFramework 4.2 packages. And DbContext inheritnace declared in DomainModel project. It's primitive:

    namespace DomainModel
    {
        class TestContext:DbContext
        {
            public DbSet<Product> Products { get; set; }
    
            public TestContext()
            {
                Database.SetInitializer<TestContext>(new DropCreateDatabaseIfModelChanges<TestContext>());
            }
        }
    }
    

    Then there are console project in solution that create TestContext and try to add some new Product to Products set. After debug start console app not responding and in step-by-step debuging fails with InvalidOperationExceprion "The context cannot be used while the model is being created." on TestContext instance calling. If any Product class is renamed - all works good, but i need work with same-named classes in different namespaces. Thanks for any information that can help solve this problem!

    Friday, January 6, 2012 10:53 PM

Answers

  • Hi Dr.Alligieri,

    Because you have the same class name in two namespace, so when you create the DbSet in the TestContext, you have explicitly point which Product you are using. Please modify your 'public DbSet<Product> Products' to 'public DbSet<Domain.Product> Products' or 'public DbSet<SuperDomain.Product> Products'.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 10, 2012 6:59 AM
    Moderator

All replies

  • Hi Dr.Alligieri,

    Welcome to MSDN Forum.

    Your post seems more like a question, so I changed its type. I'm doing research on this issue, but it may need some time, thanks for your understanding. I will come back as soon as possible.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 10, 2012 2:00 AM
    Moderator
  • Hi Dr.Alligieri,

    Because you have the same class name in two namespace, so when you create the DbSet in the TestContext, you have explicitly point which Product you are using. Please modify your 'public DbSet<Product> Products' to 'public DbSet<Domain.Product> Products' or 'public DbSet<SuperDomain.Product> Products'.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 10, 2012 6:59 AM
    Moderator