The navigation property is not a declared property on type during Code First migration with EF6 and MVC 5

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

Question

I'm trying to add new table into existing database. The database is auto-created by MVC 5 project. After trying many different things I didn't succeed in adding table Post into my database.

When I run:

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

I get an error saying: The navigation property 'PostText' is not a declared property on type 'Post'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

I don't understand this error, bacause PostText it's not a navigation property and I'm not sure why Entity Framework thinks it is.

This is definition of my Post class, within the class I have PostContext class also:

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

I've also created mapping class 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");
        }
    }

And I'm trying to do simple migration of database calling:

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

But on a Enabling migration I get mentioned error. Does someone knows what I'm doing wrongly?

Edit:

This is connectionString inside Web.config file

 <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>
1
5
1/15/2015 9:49:18 AM

Popular Answer

I'm pretty sure that your entity configuration in the issue.

The calls to HasOptional() and HasRequired() are used to specify the relation constraints. What you want to do is to set the property constraints:

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

Please note that calling ToTable() once is enough!

Here's some reading about it: Configuring/Mapping Properties and Types with the Fluent API

Also you might get the same result by using annotation properties:

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

Here's some reading about that: Code First Data Annotations

Edit:

I just saw that you want to use the table to store binary data (both byte[] properties). I don't really recommend to do this and often storing the file on disk is better (easier to implement path based caching for example). If you want to stick to this you still might want to read this SO question: MVC Model How to make byte[] nullable?

22
5/23/2017 12:34:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow