0

I have a DbContext like this,

     public class EPDContext : TrackerContext
            {
             public EPDContext()
                    : base("name=DevelopmentApplicationServices")
                {
                    Database.SetInitializer<EPDContext>(new EPDDBInitializer());

                    this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
                }

               public DbSet<TaskRevision> TaskRevisions { get; set; }
               protected override void OnModelCreating(DbModelBuilder modelBuilder)
               {
                base.OnModelCreating(modelBuilder);

                modelBuilder.Entity<TaskRevision>().HasMany(x => x.Models).WithMany().Map(x => x.MapLeftKey("TaskRevisionID").MapRightKey("ModelId").ToTable("TaskRevision2Models"));


               }
        }



  public class EPDDBInitializer : CreateDatabaseIfNotExists<EPDContext>
        {
            protected override void Seed(EPDContext context)
            {
                    //// My Seeding data goes here
                    base.Seed(context);
            }
        }

And my Entity:

 [TrackChanges]
    public class TaskRevision
    {
        #region properties
        [Key]
        public Guid TaskRevisionID { get; set; } 


        public virtual List<Model> Models { get; set; }
   }

and my migration configuration class looks like:

internal sealed class Configuration : DbMigrationsConfiguration<PW.EPD.Data.EPDContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(PW.EPD.Data.EPDContext context)
        {

        }
    }

I got this error "There is already an object named 'TaskRevisions' in the database entity framework." when I execute my application. DB has created successfully and there is no seeding data.

At the same time when I execute the same code after removing the onModelCreating() override method, db has created with seed data.

What I did wrong here, kindly correct me. Thanks in advance

Karthikeyan
  • 299
  • 1
  • 10
  • 25

0 Answers0