liveBook · Manning

EnsureCreated

As explained in Building Distributed Applications that Work

EnsureCreated is an Entity Framework method that creates the database if it does not already exist.

Entity Framework’s EnsureCreated() creates the database if it does not already exist. It uses the default database configuration, so it may not suit setups that require choices such as a specific collation. section 3.3.2

Listing 3.7 Adding data to the database via a DbContext
using (var scope = app.Services.CreateScope())
{
    var context = scope
        .ServiceProvider
        .GetRequiredService<ProductsDbContext>();   #1

    context.Database.EnsureCreated();     #2

    if (!context.Products.Any())     #3
    {
        var products = new List<Product>
        {
            new Product
            {
                Title = "Wireless Optical Mouse",
                Summary = "Ergonomic wireless optical mouse with adjustable DPI and long battery life, suitable for everyday office and home use.",
                Price = 24.99m,
                DateAdded = new DateTime(2025, 1, 5)
            },
            new Product
            {
                Title = "Mechanical Gaming Keyboard",
                Summary = "RGB backlit mechanical keyboard with blue switches, anti-ghosting keys, and durable aluminum frame.",
                Price = 129.99m,
                DateAdded = new DateTime(2025, 1, 6)
            },
            new Product
            {
                Title = "27-inch 4K Monitor",
                Summary = "27-inch UHD 4K monitor with IPS panel, 3840x2160 resolution, HDR support, and ultra-thin bezels.",
                Price = 399.00m,
                DateAdded = new DateTime(2025, 1, 7)
            },
            new Product
            {
                Title = "USB-C Docking Station",
                Summary = "Multi-port USB-C docking station with HDMI, DisplayPort, Ethernet, USB 3.0 ports, and 100W power delivery.",
                Price = 179.50m,
                DateAdded = new DateTime(2025, 1, 8)
            },
            new Product
            {
                Title = "External SSD 1TB",
                Summary = "Portable 1TB external SSD with USB 3.2 Gen 2 support, delivering fast read/write speeds in a compact design.",
                Price = 149.99m,
                DateAdded = new DateTime(2025, 1, 9)
            },
            new Product
            {
                Title = "Noise-Cancelling Headphones",
                Summary = "Over-ear wireless headphones with active noise cancellation, high-fidelity sound, and 30-hour battery life.",
                Price = 249.00m,
                DateAdded = new DateTime(2025, 1, 10)
            },
            new Product
            {
                Title = "Webcam Full HD 1080p",
                Summary = "Full HD 1080p webcam with built-in microphone, autofocus, and low-light correction for video conferencing.",
                Price = 69.99m,
                DateAdded = new DateTime(2025, 1, 11)
            },
            new Product
            {
                Title = "Gaming Laptop Backpack",
                Summary = "Water-resistant backpack designed for gaming laptops up to 17 inches, featuring padded compartments and USB charging port.",
                Price = 59.95m,
                DateAdded = new DateTime(2025, 1, 12)
            },
            new Product
            {
                Title = "Wi-Fi 6 Router",
                Summary = "Dual-band Wi-Fi 6 router offering high-speed wireless connectivity, improved range, and support for multiple devices.",
                Price = 199.00m,
                DateAdded = new DateTime(2025, 1, 13)
            },
            new Product
            {
                Title = "Portable Laser Printer",
                Summary = "Compact monochrome laser printer suitable for small offices, offering fast printing speeds and wireless connectivity.",
                Price = 289.99m,
                DateAdded = new DateTime(2025, 1, 14)
            }
        };

        context.Products.AddRange(products);    #4
        context.SaveChanges();    #5
    }
}

See the initialization sequence: resolve ProductsDbContext, call EnsureCreated(), then check whether the Products set needs initial data. section 3.3.2

How Building Distributed Applications that Work covers it

How deep it goes
Defines automatic creation when the database is absent and stops short of customized setup such as collation.
Examples
Shows EnsureCreated() before checking whether the Products set contains rows.
What it assumes
Assumes a ProductsDbContext resolved through dependency injection and does not teach database setup from zero.
What you can do after
Apply it through Entity Framework to create a missing database with default configuration.
How it connects
Connects database initialization to Entity Framework, DbContext, and dependency injection.

Also explained in

Compare how 2 books explain EnsureCreated →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.