Scaffolding error during View generation

Asked

Viewed 1,778 times

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

3 answers

3

This is really a mistake and troubled 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 Dbset to Idbset after one Clean and a Rebuild and try to generate the view, which will work. No need to mess with Mappings.

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 mean this configuration:

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

It already existed, and that Power Tools will not generate it again.

It is just an orientation. Not an error message. The configuration is correct.

  • 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.

  • 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.

  • 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.

  • Got it. Well, I can improve the answer, but you’ll have to abandon these entity configuration files.

0

Try to clear the Componentmodelcache, the cache will fix in the next rebuild.

  1. Close the Visual Studio
  2. Delete the Componentmodelcache folder C: Users Appdata Local Microsoft Visualstudio 14.0 Componentmodelcache
  3. Restart Visual Studio

Note: 14.0 is for visual studio 2015. Also works for other versions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.