I have multiple classes used in another VS project that are interconnected, e.g.:
namespace MyOtherProject
{
public class A {
public string Name {get;set;}
}
public class B {
public int Value {get;set;}
public A Child {get;set;}
}
}
Now I want to use these classes as POCO for code first in EF. However I don't need all classes in the database but only some of them. So I created a context:
public class MyContext : DbContext
{
public MyContext () : base("MyContext ")
{
}
public DbSet<B> Bs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
When I try to run the project I get this error:
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: MyProject.A: Name: Each type name in a schema must be unique. Type name 'A' is already defined.
Note that the namespace in the error is not the same as the definition of the class A because my class A is defined in another (shared) project. Instead the namespace is the one of the context.
You could have omitted the DbSet and DbSet statements and it would work the same. The Entity Framework would include them implicitly because the Student entity references the Enrollment entity and the Enrollment entity references the Course entity.
My assumption (not confirmed) is that EF sees that B references A but there is no dataset of type A, so it generates internally a class A and then suddenly finds out there are two such classes.
Anyone know what the exact problem is and what I can do to fix it? I have too many classes that I don't really need, so I don't want to create datasets for each class I have.