首先在所有以前的项目中使用数据库后,我现在正在尝试Code First。我已在web.config
编辑了连接字符串以使用"用户ID = ...;密码= ..."而魔法发生得很漂亮;生成数据库架构!但是:
Configuration
课程中精心创建的种子数据。在我的开发环境中,当我运行update-database
时,种子数据工作正常。答案 0 :(得分:0)
你可以follow this link。或者,这对我有用。这是我的.config文件中的连接字符串:
<connectionStrings>
<add name="ConnectionStringName" connectionString="data source=dataSource;initial catalog=initialCatalog;user id=userName;password=password;MultipleActiveResultSets=True;App=AppName" providerName="System.Data.SqlClient" />
</connectionStrings>
这是初始化:
public class SchoolDBInitializer : DropCreateDatabaseAlways<SchoolDBContext>
{
protected override void Seed(SchoolDBContext context)
{
IList<Standard> defaultStandards = new List<Standard>();
defaultStandards.Add(new Standard() { StandardName = "Standard 1", Description = "First Standard" });
defaultStandards.Add(new Standard() { StandardName = "Standard 2", Description = "Second Standard" });
defaultStandards.Add(new Standard() { StandardName = "Standard 3", Description = "Third Standard" });
foreach (Standard std in defaultStandards)
context.Standards.Add(std);
base.Seed(context);
}
}
以下是上下文:
public class SchoolContext : DbContext
{
public SchoolContext()
: base("SchoolDBConnectionString")
{
Database.SetInitializer<SchoolContext>(new SchoolDBInitializer<SchoolContext>());
}
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
}
这也是good link。
答案 1 :(得分:0)
好的,其中一部分是&#34; D&#39;哦!&#34;,部分不是那么多。