我遇到了同样的问题,但这个链接让我走上了至少对我有用的东西。我希望这至少可以帮助某人!:)
- 创建数据库
- 将 aspnet 表添加到新数据库
- 修复 web.config 中的数据库连接,使其指向同一个数据库
- 编写一些 sql 删除所有表,除了以“aspnet_”开头的表
- 将sql添加到您自己编写的数据库初始化程序中
- 在 Global.asax.cs 中添加对数据库初始化程序的调用
1.创建数据库
我通常使用 SQL Server Management Studio 执行此操作。我用于此示例代码的数据库是 SQL Server 2008R2,但我对您使用的 SQL Server Express 做了同样的事情。
2. 将 aspnet 表添加到新数据库
我使用以下工具,如果您在没有任何命令行参数的情况下使用它,它就像一个向导一样工作。%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regsql.exe
3.修复数据库连接,使其指向同一个数据库
以下两行来自我制作的测试应用程序。请注意,第二个连接字符串 (MyHealthContext) 的名称与我用于代码第一类的 DbContext 的名称相同。
数据库上下文:
public class MyHealthContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<PersonAttribute> PeopleAttributes { get; set; }
}
网页配置
<add name="ApplicationServices" connectionString="Server=localhost\mssql2008r2;Database=MyHealth;Integrated Security=True;" providerName="System.Data.SqlClient"/>
<add name="MyHealthContext" connectionString="Server=localhost\mssql2008r2;Database=MyHealth;Integrated Security=True;" providerName="System.Data.SqlClient"/>
4. 删除除 aspnetdb-tables 之外的所有 SQL
DECLARE @cmdDropConstraints VARCHAR(4000)
DECLARE @cmdDropTables VARCHAR(4000)
-- ======================================================================
-- DROP ALL THE FOREIGN KEY CONSTRAINTS FROM THE TABLES WE WANT TO DROP
-- ======================================================================
DECLARE cursorDropConstraints CURSOR FOR
SELECT
'ALTER TABLE ['+ s.name + '].[' + t.name + '] DROP CONSTRAINT [' + f.name +']'
FROM
sys.foreign_keys f
INNER JOIN sys.tables t ON f.parent_object_id=t.object_id
INNER JOIN sys.schemas s ON t.schema_id=s.schema_id
WHERE
t.is_ms_shipped=0
AND t.name NOT LIKE 'aspnet_%'
AND t.name <> 'sysdiagrams'
OPEN cursorDropConstraints
WHILE 1=1
BEGIN
FETCH cursorDropConstraints INTO @cmdDropConstraints
IF @@fetch_status != 0 BREAK
EXEC(@cmdDropConstraints)
END
CLOSE cursorDropConstraints
DEALLOCATE cursorDropConstraints;
-- ======================================================================
-- DROP ALL THE RELEVANT TABLES SO THAT THEY CAN BE RECREATED
-- ======================================================================
DECLARE cursorDropTables CURSOR FOR
SELECT
'DROP TABLE [' + Table_Name + ']'
FROM
INFORMATION_SCHEMA.TABLES
WHERE
Table_Name NOT LIKE 'aspnet_%'
AND TABLE_TYPE <> 'VIEW'
AND TABLE_NAME <> 'sysdiagrams'
OPEN cursorDropTables
WHILE 1=1
BEGIN
FETCH cursorDropTables INTO @cmdDropTables
IF @@fetch_status != 0 BREAK
EXEC(@cmdDropTables)
END
CLOSE cursorDropTables
DEALLOCATE cursorDropTables;
5. 数据库初始化代码:
将下面的“SQL CODE GOES HERE”替换为第 4 步中的 sql
public class MyHealthInitializerDropCreateTables : IDatabaseInitializer<MyHealthContext>
{
public void InitializeDatabase(MyHealthContext context)
{
bool dbExists;
using (new TransactionScope(TransactionScopeOption.Suppress))
{
dbExists = context.Database.Exists();
}
if (dbExists)
{
// Remove all tables which are specific to the MyHealthContext (not the aspnetdb tables)
context.Database.ExecuteSqlCommand(@"SQL CODE GOES HERE");
// Create all tables which are specific to the MyHealthContext (not the aspnetdb tables)
var dbCreationScript = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
context.Database.ExecuteSqlCommand(dbCreationScript);
Seed(context);
context.SaveChanges();
}
else
{
throw new ApplicationException("No database instance");
}
}
protected virtual void Seed(MyHealthContext context)
{
//TODO: Add code for seeding your database with some initial data...
}
}
6. 挂钩新数据库初始化程序的代码
为了确保自定义数据库初始化程序不会在生产环境中意外运行,我添加了一个#if DEBUG 语句,因为我总是在发布之前以发布模式编译我的代码。
protected void Application_Start()
{
//TODO: Comment out this database initializer(s) before going into production
#if DEBUG
Database.SetInitializer<MyHealthContext>(new MyHealthInitializerDropCreateTables()); // Create new tables in an existing database
#endif
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}