Wednesday, March 20, 2019

asp.net core EF core migrations

1) add a model
    public class TestEntity
    {
        public int Id { get; set; }

        public string TestString { get; set; }

    }

2) add a context
    public class TestContext1 : DbContext
    {
        DbSet<TestEntity> TestEntities { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder.UseSqlServer(@"Server=localhost\sqlexpress;Database=test;Trusted_Connection=True;");
            }
         

        }
    }

3) start Nuget Package Manager Console: ALT T N O
select the project with your model and context as start project
Add-Migration M0001
update-database

WICHTIG: Projekt mit Context muß als Startup projekt und in Nuget Manager Console ausgewählt sein ! Bei Asp.Net muß der Context auch in der Startup Klasse sein ...

No comments: