Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 969
nemo_87 Avatar asked Jan 15 '15 09:01

nemo_87


1 Answers

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?

like image 59
pysco68 Avatar answered Oct 08 '22 17:10

pysco68