-1

I am not familiar with EF and just following examples of a WebAPI book.

This is the error I get:

enter image description here

And this is the DbContext class I have:

namespace SportsStore.Models
{
    public class ProductDbContext: DbContext
    {
        public ProductDbContext()
            : base("SportsStoreDb")
        {
            Database.SetInitializer<ProductDbContext>(new ProductDbInitializer());
        }

        public DbSet<Product> Products { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderLine> OrderLines { get; set; }
    }
}

and there is also an Initializer class like this:

using System.Collections.Generic;
using System.Data.Entity;

namespace SportsStore.Models
{
    public class ProductDbInitializer : DropCreateDatabaseAlways<ProductDbContext>
    {
        protected override void Seed(ProductDbContext context)
        {
            new List<Product> {
                new Product() { Name = "Kayak", Description = "A boat for one person",
                    Category = "Watersports", Price = 275m },
                new Product() { Name = "Lifejacket",
                    Description = "Protective and fashionable",
                    Category = "Watersports", Price = 48.95m },
                new Product() { Name = "Soccer Ball",
                    Description = "FIFA-approved size and weight",
                    Category = "Soccer", Price = 19.50m },
                new Product() {
                    Name = "Corner Flags",
                    Description = "Give your playing field a professional touch",
                    Category = "Soccer", Price = 34.95m },
                new Product() { Name = "Stadium",
                    Description = "Flat-packed 35,000-seat stadium",
                    Category = "Soccer", Price = 79500m },
                new Product() { Name = "Thinking Cap",
                    Description = "Improve your brain efficiency by 75%",
                    Category = "Chess", Price = 16m },
                new Product() { Name = "Unsteady Chair",
                    Description = "Secretly give your opponent a disadvantage",
                    Category = "Chess", Price = 29.95m },
                new Product() { Name = "Human Chess Board",
                    Description = "A fun game for the family",
                    Category = "Chess", Price = 75m },
                new Product() { Name = "Bling-Bling King",
                    Description = "Gold-plated, diamond-studded King",
                    Category = "Chess", Price = 1200m },
            }.ForEach(product => context.Products.Add(product));

            context.SaveChanges();

            new List<Order> {
                new Order() { Customer = "Alice Smith", TotalCost = 68.45m,
                    Lines = new List<OrderLine> {
                        new OrderLine() { ProductId = 2, Count = 2},
                        new OrderLine() { ProductId = 3, Count = 1},
                    }},
                new Order() { Customer = "Peter Jones", TotalCost = 79791m,
                    Lines = new List<OrderLine> {
                        new OrderLine() { ProductId = 5, Count = 1},
                        new OrderLine() { ProductId = 6, Count = 3},
                        new OrderLine() { ProductId = 1, Count = 3},
                   }}
            }.ForEach(order => context.Orders.Add(order));

            context.SaveChanges();
        }
    }
}

And I was able to login to localhost db of my SQL Server engine too .

So what should I be looking at to solve the issue?

Bohn
  • 26,091
  • 61
  • 167
  • 254
  • Possible duplicate of [SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance](https://stackoverflow.com/questions/26248293/sql-network-interfaces-error-50-local-database-runtime-error-occurred-canno) – CodeCaster Aug 12 '19 at 11:06
  • Screen shot is better! Picture worth a thousand words ! And the code is attached too ... what else do you need – Bohn Aug 12 '19 at 11:11
  • That's not a duplicate. That one is using connection strings. Mine is EF Code First, there is not even a connection string in mine. – Bohn Aug 12 '19 at 11:11
  • The book literally explains that in Code First approach you don't need Connection Strings. Once it his the base class it will create the schema and database for us. If you don't know the answer that's fine. Move on, let other people give their thoughts. – Bohn Aug 12 '19 at 11:15
  • Looks to me like it is trying to create the db in SQL Express which can happen in scenarios laid out [here](https://learn.microsoft.com/en-us/ef/ef6/fundamentals/configuring/connection-strings). I find it better to take control and use an explicit connection string to a known database rather than relying on a changing convention. – Steve Greene Aug 12 '19 at 17:14

1 Answers1

0

Ok finally solved it myself. Yeah nothing with connection string. Glad I didn't go to that rabbit hole that people were posting as duplicate answer.

The problem was in Web.Config the version below was 13, I changed it to 11 now it works.

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>
Bohn
  • 26,091
  • 61
  • 167
  • 254
  • Probably why it is phrased "possible duplicate". I have also seen this behavior and connection strings ALSO solve the issue and give you more control. A year from now when somebody comes by and changes that code (or upgrades to ef core) it will be wtf? – Steve Greene Aug 13 '19 at 01:55