0

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

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
  • Have you added a migration? http://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx – Hans Kilian Jan 09 '19 at 17:19
  • When you debug are you hitting the initializer code? BTW, I would move your initializer to a static constructor - see [here](https://stackoverflow.com/questions/16727585/database-setinitializer-to-null-not-working-entity-framework-4-3-1-code-first#16727643). Double check your connect string then I would try removing the database and see if that works. @HansKilian Migrations are not needed if he is using an initializer. In fact, in early development I prefer initializers and seeding over migrations. – Steve Greene Jan 09 '19 at 18:23
  • Yes, the initializer gets hit. If I remove the database through management studio, the log in that EF is going to use disappears too, no? When I intialiity set it up, I created a blank database and created a user for EF to use. On the first run, it created the tables I had as classes at the time. But subsequent runs do nothing to the schema. – Christopher Pisz Jan 10 '19 at 21:56
  • If I delete the database and run the program. The database is recreated, but I still see no Stations table or field called Location. The Name field is there though. Also, I can add a field of type string and call it Poop, and then that shows up. So, I assume it doesn't know how to create the a Field for the Vector3. – Christopher Pisz Jan 14 '19 at 21:03

1 Answers1

0

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

Amin Sahranavard
  • 268
  • 1
  • 10
  • So is Table per Hierarchy the default and will the physical table always be named after the base? Because I haven't set up anything to dictate the inheritance strategy yet. – Christopher Pisz Jan 09 '19 at 19:15
  • Also, I looked and the columns for station's fields are not present in the manufacturers table. I will update the screenshot to show this in the OP – Christopher Pisz Jan 09 '19 at 20:07
  • You don’t have to do anything special in Code First to enable TPH. It's the default inheritance mapping strategy – Amin Sahranavard Jan 09 '19 at 21:02