简体   繁体   中英

Is there a way to create db without add data to it ef code-first

I started to use the code-first approach with entity framework, When I launch my console app I saw my new data base created in my sql server databases folder, but it created only when I pushed to the data base some data

static void Main(string[] args)
        {
            var db = new MainModel();

            Product prod = new Product() { ProductName = "milk", ProductPrice = 4 };

            db.Products.Add(prod);
            db.SaveChanges();

            Console.WriteLine("done!");
            Console.ReadLine();

        }

Now when I didn`t add any data the database not create. Is there a way to initial db with code-first approach without pushing data into it?

1) Open cmd

2) Go to project folder (by cd .../yourfolder )

3) Type dotnet ef migrations add MyFirstMigration

4) Then type dotnet ef database update

for more details, you can visit here

You should create dbContext class:

{
    public MyContext (DbContextOptions<MyContext > options)
        : base(options)
    { }

    public DbSet<Product> Products{ get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM