4

I am trying to add a view using a viewmodel as the Model class to my MVC project, but when I do I get a popup with this error:

"Error

There was an error running the selected code generator: 'A configuration for type Blog.Domain.Entities.Post' has already been added. To reference the existing configuration use the Entity() or ComplexType() methods.'"

It does not throw an error when I use the actual entity though (Blog.Domain.Entities.Post).

The viewmodel: (its the same with ANY viewmodel though)

public class PostViewModel
    {
        public string Title { get; set; }

        public string Description { get; set; }

        public string UrlSlug { get; set; }

        public DateTime PostedOn { get; set; }

        public Category Category { get; set; }

        public ICollection<Tag> Tags { get; set; } 
    }

The Post entity:

public class Post : IEntity
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public string Meta { get; set; }

        public string UrlSlug { get; set; }

        public bool Published { get; set; }

        public DateTime PostedOn { get; set; }

        public DateTime? Modified { get; set; }

        public virtual Category Category { get; set; }

        public virtual ICollection<Tag> Tags { get; set; }
    }

My custom DbContext:

public class BlogDbContext : DbContext
    {
        public BlogDbContext() : base("Name=BlogDbContext")
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BlogDbContext>());
        }

        public DbSet<Post> Posts { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Tag> Tags { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new PostMap());
            modelBuilder.Configurations.Add(new CategoryMap());
            modelBuilder.Configurations.Add(new TagMap());
        }
    }

EF mapping for the Post entity:

public class PostMap : EntityTypeConfiguration<Post>
    {
        public PostMap()
        {
            HasKey(m => m.Id);

            Property(m => m.Title)
                .HasMaxLength(256)
                .IsRequired();

            Property(m => m.Description)
                .HasMaxLength(5000)
                .IsRequired();

            Property(m => m.Meta)
                .HasMaxLength(1024)
                .IsRequired();

            Property(m => m.UrlSlug)
                .HasMaxLength(256)
                .IsRequired();

            Property(m => m.Published)
                .IsRequired();

            Property(m => m.PostedOn)
                .IsRequired();

            Property(m => m.Modified)
                .IsOptional();

            HasRequired(m => m.Category)
                .WithMany(c => c.Posts);

            HasMany(m => m.Tags)
                .WithMany(t => t.Posts)
                .Map(mc =>
                {
                    mc.ToTable("PostTagMap");
                    mc.MapLeftKey("PostId");
                    mc.MapRightKey("TagId");
                });
        }
    }

I can not figure out why it would give me that error, the viewmodel should have nothing to do with EF whatsoever. Halpmeh!

  • Dunno (EF, yech), but it makes little sense to create a VM that replaces a model. You should be able to expose your Post(s) directly through a collection or properties on a ViewModel. –  Mar 09 '15 at 16:52
  • 3
    Actually, it makes a lot of sense. Regardless, that is not the issue, and it doesn't work either way. – Tomas Hjelm Mar 09 '15 at 20:12
  • After adding the ViewModel and trying to create a view, I got the same error. I then cancelled out, debugged/built, and immediately quick debugging after a successful start. The second time, I did *not* pick a "data context class" (not sure if that's important), and it worked. YMMV. – ruffin Mar 20 '15 at 18:39

0 Answers0