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>
nemo_87
  • 4,523
  • 16
  • 56
  • 102

4 Answers4

24

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?

Community
  • 1
  • 1
pysco68
  • 1,136
  • 9
  • 15
  • Thanks for answering, after all it was some messy mistake that happens only inside that project. I've tried this in brand new project and it's working properly...so who know what the real error is...I also think that is inside Configuration file... – nemo_87 Jan 15 '15 at 10:24
  • I am pretty sure the issue actually was your code in `PostConfiguration : EntityTypeConfiguration`. As I wrote, using `HasRequired()` is related to NavigationProperties thus EF's assuption. – pysco68 Jan 15 '15 at 10:38
  • 1
    Property(c => c.PostText).IsRequired(); yes, yes, yes! – Teoman shipahi Jun 28 '15 at 04:37
  • Yep, this error was caused by saying model.HasRequired(m => m.PostText). This is incorrect. The corrected code is model.Property(p => p.PostText).IsRequired() – Judah Gabriel Himango Sep 01 '15 at 19:20
  • @pysco68 It helped me, I configured it wrong. Thanks. – Arun Prasad E S Dec 21 '16 at 16:27
0

Similar situation occurred to me in fluent API

This lead to error.

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

I changed to

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

The ID was swithced.

Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
0

The second part of the message: "Verify that it has not been explicitly excluded from the model and that it is a valid navigation property" - Check if someone(or you) accidentally ignore that model in Ignore method.

private void Ignore(DbModelBuilder modelBuilder)
{
     modelBuilder.Ignore<SomeModel>();
}
nemostyle
  • 794
  • 4
  • 11
  • 32
0

Check in your class reference to put virtual attribute to the class.

mariobot
  • 49
  • 1
  • 5