Entity Framework 6 - Database schema is not updating

c# entity-framework-6

Question

I am learning EF I wanted to play with the different types of inheritance using code first.

I initially started off with a few classes and ran my application. I saw the database get created with all my classes represented as tables.

However, when I add new classes or fields and run the application again, I do not see the changes in my database schema.

I have used the "DropCreateDatabaseAlways", so I don't understand why my database is not being updated with the proper schema as I add fields and classes. Can someone explain what I am doing wrong?

Initial Code:

namespace Domain
{
    public class Good
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double BaseValue { get; set; }
    }

    public class Manufacturer {
        public int Id {get; set;}
        public ICollection<ManufacturingCapability> ManufacturingCapabilities { get; set; }
    }

    public class ManufacturingCapability {
        public int Id { get; set; }
        public ICollection<ManufacturingInput> Inputs { get; set; }
        public ICollection<ManufacturingOutput> Outputs { get; set; }
        public TimeSpan CycleTime { get; set; }
    }

    public class ManufacturingInput {

        public int Id { get; set; }
        public Good Good { get; set; }
        public uint Quantity { get; set; }
    }

    public class ManufacturingOutput {
        public int Id { get; set; }
        public Good Good { get; set; }
        public uint Quantity { get; set; }
    }

    public class ManufacturingDbContext :DbContext {

        public DbSet<Good> Goods { get; set; }
        public DbSet<Manufacturer> Manufacturers { get; set; }

        public ManufacturingDbContext() : base("name=EFLearnConnectionString") {
            Database.SetInitializer<ManufacturingDbContext>(new DropCreateDatabaseAlways<ManufacturingDbContext>());
        }
    }
}

namespace EFLearning {
    class Program {
        static void Main(string[] args) {
            using (var manufacturingDbContext = new ManufacturingDbContext()) {
                var good = new Good() { Name = "Water" };

                manufacturingDbContext.Goods.Add(good);
                manufacturingDbContext.SaveChanges();
            }
        }
    }
}

Database Tables in Management Studio after running: enter image description here

Added code:

public class Station : Manufacturer {
    public string Name { get; set; }
    public Vector3 Location { get; set; }
}

and I added this to context:

public DbSet<Station> Stations { get; set; }

Database Tables in Management Studio after running: enter image description here

1
0
1/9/2019 8:08:56 PM

Popular Answer

Your strategy for domain design to station and manufactor entity is Table per Hierarchy.so stations properties are now in manufactors table. go to this link for more information.

if you you want Station entity have a seperate table must use of Table per Type go to this link for Table per Type

0
1/9/2019 6:39:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow