我对EF
并不熟悉,只是对WebAPI
一本书的示例不熟悉。
这是我得到的错误:
这是我拥有的DbContext
类:
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; }
}
}
还有一个类似这样的Initializer类:
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();
}
}
}
我也能够登录到SQL Server引擎的本地主机数据库。
那我应该怎么解决这个问题?
答案 0 :(得分:0)
Ok终于自己解决了。是的,没有连接字符串。很高兴我没有去找别人重复回答的那个兔子洞。
问题出在Web中。配置以下版本为13,现在将其更改为11。
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>