Scaffolding error during View generation

3

I'm creating a new project using EF Power Tools and I get 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 work around the error (Generating View without a context), but I need to understand what I did wrong.

My entity follows:

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);

    }
}

Context 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());
    }
}
    
asked by anonymous 24.11.2015 / 12:53

3 answers

2

This error message:

  

'A configuration for type' TST2.Models.Course 'has already been added. To reference the existing configuration use the Entity () or ComplexType () methods.

You want this setting:

public class CourseMap : EntityTypeConfiguration<Course>
{ ... }

It already existed, and Power Tools will not regenerate it.

It's just an orientation. Not an error message. The setting is correct.

    
24.11.2015 / 14:51
3

This is really a mistake and it disturbed me for a long time, I solved it the following way only so it worked with me. You have to change where you are, for example:

public DbSet<OnsiteCourse> OnsiteCourses { get; set; }

To:

public IDbSet<OnsiteCourse> OnsiteCourses { get; set; }

Change the DbSet to IDbSet after a Clean and Rebuild and try to generate the view, which will work. No need to mess with Mappings.

    
23.11.2016 / 16:48
0

Try to clean the ComponentModelCache, the cache will fix in the next rebuild.

  • Close Visual Studio
  • Delete the ComponentModelCache folder C: \ Users \ AppData \ Local \ Microsoft \ VisualStudio \ 14.0 \ ComponentModelCache
  • Restart Visual Studio
  • Note: 14.0 is for visual studio 2015 version. Also works for other versions

        
    17.05.2017 / 22:00