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

标签 c# asp.net-mvc asp.net-mvc-5 entity-framework-6 entity-framework-migrations

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

当我运行时:

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

我收到一条错误消息:导航属性“PostText”不是类型“Post”的已声明属性。确认它没有被明确排除在模型之外,并且它是一个有效的导航属性。

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

这是我的 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 文件中的 connectionString

 <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() 一次就足够了!

这里有一些关于它的读物: Configuring/Mapping Properties and Types with the Fluent 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; }
}

这里有一些关于它的读物: Code First Data Annotations

编辑:

刚刚看到你想用表来存储二进制数据(都是byte[]属性)。我真的不建议这样做,通常将文件存储在磁盘上会更好(例如,更容易实现基于路径的缓存)。如果你想坚持这一点,你仍然可能想阅读这个 SO 问题:MVC Model How to make byte[] nullable?

关于c# - 在使用 EF6 和 MVC 5 进行代码优先迁移期间,导航属性不是类型的声明属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27960694/

相关文章:

c# - Microsoft Practice 使用 Unity 寄存器类型

javascript - jQuery Click 事件 CheckBox 改变 Ajax 调用

c# - 在 C# 中的运行时向类型化对象添加 expando 属性

c# - 将 C# 项目从 VS2010 升级到 VS2012

c#和ffpmeg命令行问题

c# - 在 dev 中使用 UpdatePanel 时 Div show hide 不起作用

c# - 如何仅发布 MVC 列表中选定的项目

c# - 在 MVC 中使用 Json.NET 自动将 mongodb ObjectId 重新调整为字符串

asp.net-mvc - Html.DisplayFor 未将值发布到 ASP.NET MVC 3 中的 Controller

c# - 某些用户的登录尝试无效