Search code examples
c#entity-frameworkexceptionentity

Schema specified is not valid exception occured when using table in multiple dlls


I have an issue as described here. Let me explain the details.

I have a table which is used for two different dll's with different edmx files. And I have an executable which uses these two dlls. When I call one of them, it throws the exception specified in the above(Schema specified is not valid, multiple types with the name...)

Can someone describe me what causes this error in this case?

Edit: Detailed explanation is below:

  1. Below code is in ABC.dll:

ABC.dll -> EntModel.edmx -> EntModel.Context.cs

namespace MyNamespaceABC
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class AbcEntities : DbContext
    {
        public AbcEntities()
            : base("name=AbcEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<MyTable> MyTables { get; set; }
    }
}
  1. Below code is in XYZ.dll:

XYZ.dll -> EntModel.edmx -> EntModel.Context.cs

namespace MyNamespaceXYZ
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class XyzEntities : DbContext
    {
        public XyzEntities()
            : base("name=XyzEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<MyTable> MyTables { get; set; }
    }
}

And these dlls are used by an executable called Main.exe. When I test these dlls on their tester projects, they work like a charm. Then, when I call it from Main.exe, both of the dlls throw this exception when I try to retrieve data.

Schema specified is not valid. Errors:

Multiple types with the name 'MyTable' exist in the EdmItemCollection in different namespaces. Convention based mapping requires unique names without regard to namespace in the EdmItemCollection.


Solution

  • To resolve this issue, I renamed the entity name in the edmx diagram and the error is gone.

    To sum up, it is forbidden to use same entity name for different projects. Using one and only structure for accessing database will resolve the issue permanently for the main project.