我一直在寻找并寻找答案,而且有多个但我无法通过解决这些问题的解决方案来解决问题。我的问题是数据没有播种到数据库。数据库工作并创建表,但不会将数据库初始化程序中的数据放入其中。
上下文类
using System.Data.Entity;
namespace FlextrafikWeb.Models
{
public class Context : DbContext
{
public Context() : base("FlextrafikWeb") { }
public DbSet<Car> Cars { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<Price> Prices { get; set; }
}
}
数据库初始化程序
using System.Collections.Generic;
using System.Data.Entity;
namespace FlextrafikWeb.Models
{
public class DatabaseInitializer : DropCreateDatabaseIfModelChanges<Context>
{
protected override void Seed(Context context)
{
GetUsers().ForEach(u => context.Users.Add(u));
GetCars().ForEach(c => context.Cars.Add(c));
GetLocations().ForEach(l => context.Locations.Add(l));
GetPrices().ForEach(p => context.Prices.Add(p));
context.SaveChanges();
}
private static List<Car> GetCars()
{
var cars = new List<Car>
{
new Car
{
CarID = 1,
GaranteeCarNumber = 2,
RegistrationNumber = 123456,
ContactTelephone = "12 34 56 78",
CarType = 1,
CarLocation = null,
CarPrice = null,
CarOwner = null,
UserID = 1,
},
new Car
{
CarID = 3,
GaranteeCarNumber = 4,
RegistrationNumber = 654321,
ContactTelephone = "87 65 43 21",
CarType = 2,
CarLocation = null,
CarPrice = null,
CarOwner = null,
UserID = 2,
},
};
return cars;
}
private static List<User> GetUsers()
{
var users = new List<User>
{
new User
{
UserID = 1,
UserName = "Firma A",
CompanyCVR = 12345678
},
new User
{
UserID = 2,
UserName = "Firma B",
CompanyCVR = 98765432
}
};
return users;
}
private static List<Location> GetLocations()
{
var locations = new List<Location>
{
new Location
{
LocationID = 1,
StreetName = "Testgade",
StreetNumber = 4,
PostalCode = 5000,
City = "Odense",
Municipality = "Odense",
},
new Location
{
LocationID = 2,
StreetName = "Testvej",
StreetNumber = 34,
PostalCode = 5300,
City = "Kerteminde",
Municipality = "Kerteminde",
}
};
return locations;
}
private static List<Price> GetPrices()
{
var prices = new List<Price>
{
new Price
{
PriceID = 1,
StartupFeeWeekday = 30.00,
RunningRateWeekday = 330.00,
WaitingRateWeekday = 250.00,
StartupFeeEvening = 60.00,
RunningRateEvening = 500.00,
WaitingRateEvening = 400.00,
StartupFeeWeekend = 600.00,
RunningRateWeekend = 500.00,
WaitingRateWeekend = 400.00,
},
new Price
{
PriceID = 2,
StartupFeeWeekday = 10.00,
RunningRateWeekday = 249.00,
WaitingRateWeekday = 249.00,
StartupFeeEvening = 20.00,
RunningRateEvening = 269.00,
WaitingRateEvening = 269.00,
StartupFeeWeekend = 20.00,
RunningRateWeekend = 269.00,
WaitingRateWeekend = 269.00,
},
};
return prices;
}
}
}
全局
using System.Web.Security;
using System.Web.SessionState;
using System.Data.Entity;
using FlextrafikWeb.Models;
namespace FlextrafikWeb
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// Initialize the product database.
Database.SetInitializer(new DatabaseInitializer());
}
}
}
我不知道你还需要什么,但我似乎无法想出任何作品。
答案 0 :(得分:2)
这里有两件事:
本教程应为您提供更多详细信息http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx
希望这有帮助
答案 1 :(得分:1)
您应该在上下文类的构造函数中设置自定义初始化程序的使用:
using System.Data.Entity;
namespace FlextrafikWeb.Models
{
public class Context : DbContext
{
public Context() : base("FlextrafikWeb")
{
Database.SetInitializer(new DatabaseInitializer());
}
public DbSet<Car> Cars { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<Price> Prices { get; set; }
}
}