在使用 EF6 和 MVC 5 进行代码优先迁移期间,导航属性不是类型上的声明属性



我正在尝试将新表添加到现有数据库中。数据库由 MVC 5 项目自动创建。在尝试了许多不同的事情之后,我没有成功地将表格发布添加到我的数据库中。

当我运行时:

PM> Enable-Migrations -ContextTypeName StudentBookApp.Models.PostContext -Force

我收到一条错误消息:导航属性"PostText"不是类型"Post"上的声明属性。验证它是否尚未从模型中显式排除,并且它是有效的导航属性。

我不明白这个错误,因为PostText它不是导航属性,我不确定为什么实体框架认为它是。

这是我的 Post 类的定义,在该类中我还有 PostContext 类:

public class Post
{
    [Key]
    public int PostId { get; set; }
    public string PostText { get; set; }
    public byte[] ImagePost { get; set; }
    public byte[] FilePost { get; set; }
    public string TextPost { get; set; }
    public string UserId { get; set; }
}
public class PostContext : DbContext
{
    static PostContext()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<PostContext>());
    }
    public DbSet<Post> Posts { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new PostConfiguration());
    }
}

我还创建了映射类PostConfiguration:

public class PostConfiguration : EntityTypeConfiguration<Post>
    {
        public PostConfiguration() : base()
        {
            HasKey(p => p.PostId);
            ToTable("Post");
            HasRequired(p => p.PostText);
            ToTable("Post");
            HasOptional(p => p.ImagePost);
            ToTable("Post");
            HasOptional(p => p.FilePost);
            ToTable("Post");
            HasOptional(p => p.TextPost);
            ToTable("Post");
            HasRequired(p => p.UserId);
            ToTable("Post");
        }
    }

我正在尝试对数据库调用进行简单的迁移:

Enable-Migration
Add-Migration "NewMigration"
Update-Database

但是在启用迁移时,我被提及错误。有人知道我做错了什么吗?

编辑:

这是 Web.config 文件中的连接字符串

 <connectionStrings>
    <add name="PostContext" connectionString="Data Source=(LocalDb)v11.0;AttachDbFilename=|DataDirectory|aspnet-StudentBookApp-20150114035149.mdf;Initial Catalog=aspnet-StudentBookApp-20150114035149;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
我很

确定您的实体配置在问题中。

HasOptional()HasRequired() 的调用用于指定关系约束。您要做的是设置属性约束:

Property(c => c.PostText).IsRequired();

请注意,调用一次 ToTable() 就足够了!

以下是一些关于它的阅读:使用流畅的 API 配置/映射属性和类型

此外,使用注释属性可能会获得相同的结果:

public class Post
{
    [Key]
    public int PostId { get; set; }
    [Required]
    public string PostText { get; set; }
    public byte[] ImagePost { get; set; }
    public byte[] FilePost { get; set; }
    public string TextPost { get; set; }
    [Required]
    public string UserId { get; set; }
}

以下是一些关于此的内容:代码优先数据注释

编辑:

我刚刚看到您想使用该表来存储二进制数据(两个 byte[] 属性)。我真的不建议这样做,并且通常将文件存储在磁盘上更好(例如,更容易实现基于路径的缓存)。如果你想坚持这一点,你可能仍然想阅读这个SO问题:MVC模型如何使byte[]为空?

在流畅的 API 中,我也遇到了类似的情况

这会导致错误。

modelBuilder.Entity<QualityRating>()
    .HasRequired(x => x.RatedOnUserID)
    .WithMany()
    .HasForeignKey(x => x.ApplicationUser);

我改成了

modelBuilder.Entity<QualityRating>()
    .HasRequired(x => x.ApplicationUser)
    .WithMany()
    .HasForeignKey(x => x.RatedOnUserID);

该 ID 被取消。

消息的第二部分:"验证它是否未从模型中显式排除,并且它是有效的导航属性" - 检查某人(或您)是否意外忽略了 Ignore 方法中的该模型。

private void Ignore(DbModelBuilder modelBuilder)
{
     modelBuilder.Ignore<SomeModel>();
}

签入类引用以将虚拟属性放入类。

最新更新