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!