我需要创建一个已加载数据的数据库。 它没有返回任何错误,只是没有workinkg。数据库创建完美但空洞。我认为问题出在appConfig
中上下文:
namespace Domain.DAL
{
public class LandingContext : DbContext
{
public LandingContext() : base(Properties.Settings.Default.ConString)
{
Database.SetInitializer<LandingContext>(new CreateDatabaseIfNotExists<LandingContext>());
Database.Initialize(true);
}
public DbSet<Package> Packages { get; set; }
public DbSet<AbsProduct> Products { get; set; }
}
}
初始化器:
namespace Domain.DAL
{
public class LandingInitializer : CreateDatabaseIfNotExists<LandingContext>
{
protected override void Seed(LandingContext context)
{
Car kangoo = new Car(100, 2, "kangoo");
context.Products.Add(kangoo);
context.SaveChanges();
base.Seed(context);
}
}
}
appconfig:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="Domain.Properties.Settings.ConString" connectionString="Data Source=ENZO-PC;Initial Catalog=GLB-Landing;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<contexts>
<context type=" DAL.LandingContext, Domain">
<databaseInitializer type="DAL.LandingInitializer, Domain" />
</context>
</contexts>
</configuration>
答案 0 :(得分:0)
我重新放置
Database.SetInitializer<LandingContext>(new CreateDatabaseIfNotExists<LandingContext>());
for:
Database.SetInitializer<LandingContext>(new LandingInitializer());
工作......