C# 数据库初始值设定项不工作
我需要用已经加载的数据创建一个数据库。 它不返回任何错误,只是不工作。数据库创建得很完美,但为空。我认为问题出在appConfig中 背景:C# 数据库初始值设定项不工作,c#,.net,entity-framework,C#,.net,Entity Framework,我需要用已经加载的数据创建一个数据库。 它不返回任何错误,只是不工作。数据库创建得很完美,但为空。我认为问题出在appConfig中 背景: namespace Domain.DAL { public class LandingContext : DbContext { public LandingContext() : base(Properties.Settings.Default.ConString) { Database.S
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
{
公共类LandingContext:DbContext
{
public LandingContext():基本(Properties.Settings.Default.Consting)
{
SetInitializer(新的CreateDatabaseIfNotExists());
数据库初始化(true);
}
公共DbSet包{get;set;}
公共数据库集产品{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);
}
}
}
namespace Domain.DAL
{
公共类LandingInitializer:CreateDatabaseIfNotExists
{
受保护的覆盖无效种子(LandingContext上下文)
{
Car kangoo=新车(100,2,“kangoo”);
context.Products.Add(kangoo);
SaveChanges();
种子(上下文);
}
}
}
以及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>
I重新放置
Database.SetInitializer<LandingContext>(new CreateDatabaseIfNotExists<LandingContext>());
Database.SetInitializer(新的CreateDatabaseIfNotExists());
用于:
Database.SetInitializer(new LandingInitializer());
和工作…你在哪里调用方法“种子”?我应该如何调用他,在哪里?从上下文来看,我无法访问您想要的任何初始值设置位置。可能是在您获得产品后,您正在将其放入数据库中。@jdweng但是“Seed”方法受到保护,我无法访问……您为什么要使其受到保护?
Database.SetInitializer<LandingContext>(new LandingInitializer());