3
I am creating a new project using EF Power Tools and I have the following error:
Error
There was an error running the Selected code Generator:
'A Configuration for type 'TST2.Models.Course' has already been Added. To Reference the existing Configuration use the Entity() or Complextype() methods.
I can get around the error (Generating the View without a context), but I need to understand what I did wrong.
Follows my entity:
public class Course
{
public Course()
{
this.StudentGrades = new List<StudentGrade>();
this.People = new List<Person>();
}
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }
public virtual OnlineCourse OnlineCourse { get; set; }
public virtual OnsiteCourse OnsiteCourse { get; set; }
public virtual ICollection<StudentGrade> StudentGrades { get; set; }
public virtual ICollection<Person> People { get; set; }
}
Code for Mapping :
public class CourseMap : EntityTypeConfiguration<Course>
{
public CourseMap()
{
// Primary Key
HasKey(t => t.CourseID);
// Properties
Property(t => t.CourseID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(t => t.Title)
.IsRequired()
.HasMaxLength(100);
// Table & Column Mappings
ToTable("Course");
Property(t => t.CourseID).HasColumnName("CourseID");
Property(t => t.Title).HasColumnName("Title");
Property(t => t.Credits).HasColumnName("Credits");
Property(t => t.DepartmentID).HasColumnName("DepartmentID");
// Relationships
HasMany(t => t.People)
.WithMany(t => t.Courses)
.Map(m =>
{
m.ToTable("CourseInstructor");
m.MapLeftKey("CourseID");
m.MapRightKey("PersonID");
});
HasRequired(t => t.Department)
.WithMany(t => t.Courses)
.HasForeignKey(d => d.DepartmentID);
}
}
Contexto Code:
public class SchoolContext : DbContext
{
static SchoolContext()
{
Database.SetInitializer<SchoolContext>(null);
}
public SchoolContext()
: base(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=School;Integrated Security=True;MultipleActiveResultSets=True")
{ }
public DbSet<Course> Courses { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<OfficeAssignment> OfficeAssignments { get; set; }
public DbSet<OnlineCourse> OnlineCourses { get; set; }
public DbSet<OnsiteCourse> OnsiteCourses { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<StudentGrade> StudentGrades { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CourseMap());
modelBuilder.Configurations.Add(new DepartmentMap());
modelBuilder.Configurations.Add(new OfficeAssignmentMap());
modelBuilder.Configurations.Add(new OnlineCourseMap());
modelBuilder.Configurations.Add(new OnsiteCourseMap());
modelBuilder.Configurations.Add(new PersonMap());
modelBuilder.Configurations.Add(new StudentGradeMap());
}
}
Power Tools generated these models. Using scaffolding to create a View, the error message appears. I’m not trying to generate again, I’m creating a View on top of the Power Tools model.
– Tom
Yes, but the View generation tries to create the configuration again, possibly because it tries to create some step with Controller also. If you want to generate only the View, use the Scaffolding Visual Studio, which is in the right-click menus.
– Leonel Sanches da Silva
So that’s exactly what you described what I’m doing. And the mistake insists on stopping the action. As I said, if I don’t select a data context, it can create the View. It’s a way to bypass but not fix the error.
– Tom
Got it. Well, I can improve the answer, but you’ll have to abandon these entity configuration files.
– Leonel Sanches da Silva