Question

I have added

public IEnumerable<Comment> Comments { get; set; }

to a Model in an ASP.net MVC project. I ran the following to generate a migration in the package console

PM> Add-Migration AddCommentsToDevice

and the resulting migration did not pick up the change to the model

public partial class AddCommentsToDevice : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

Any tips on the syntax of the migrations or what causes detections?

Was it helpful?

Solution

You've added to little. You need to configure the relationship properly - the best with fluent api. Use this for navigation property:

public virtual ICollection<Comment> Comments { get; set; }

Important - always use ICollection, not IEnumerable for Navigation properties and make them virtual - thanks to this ef will be able to track changes.

Then in your DbContext you add following code:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Model>().HasKey(x => x.Id);
        modelBuilder.Entity<Model>().HasMany(x => x.Comments);

        base.OnModelCreating(modelBuilder);
    }

I assumed that class related to Comments is named Model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top