Visual Studio在尝试构建视图模型时出错

时间:2015-03-08 15:48:01

标签: c# entity-framework visual-studio mvvm asp.net-mvc-scaffolding

我正在尝试使用viewmodel作为Model类添加一个视图到我的MVC项目,但是当我这样做时,我得到一个带有此错误的弹出窗口:

  

"错误

     

运行所选代码生成器时出错:' A.   Blog.Domain.Entities.Post'的类型配置已经过了   添加。要引用现有配置,请使用Entity()或   ComplexType()方法。'"

当我使用实际实体时,它不会抛出错误(Blog.Domain.Entities.Post)。

视图模型: (虽然与任何视图模型相同)

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

邮政实体:

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

我的自定义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映射:

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

我无法弄清楚为什么它会给我这个错误,viewmodel应该与EF无关。 Halpmeh!

0 个答案:

没有答案