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>