MVC 3,MVC脚手架。使用 SQLCe 而不是 Express。未创建数据库文件
我一直在为我的下一个 CMS 使用新的 MVCScaffolding
,到目前为止它非常棒。太棒了,直到我想将我的 SQLExpress
默认数据库更改为 SQLCe
本地数据库。
这里( http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/ )它说如果我安装“EFCodeFirst.SqlServerCompact
”包,它将负责更改它,甚至创建数据库文件。所以我就这么做了。
它创建了一个名为“SQLCEEntityFramework.cs
”的文件,该文件中存在一些错误。就像对不再存在的“System.Data.Entity.Database
”的引用以及对现在是Database
的DbDatabase
的引用。所以我修复了这些错误并运行了该应用程序。
一切都正常运行,但没有连接字符串添加到我的 Web.config 中,并且在我的 App_Data 目录中没有创建数据库文件。所以现在我开始怀疑我是否做错了什么......
有人知道这里发生了什么以及如何解决它吗?
多谢。
编辑:以防万一您想查看 SQLCEEntityFramework.cs 文件中的内容:
using System;
using System.Data.Entity;
using System.Data.SqlServerCe;
using System.IO;
using System.Transactions;
using System.Data.Entity.Infrastructure;
[assembly: WebActivator.PreApplicationStartMethod(typeof(CMS.App_Start.SQLCEEntityFramework), "Start")]
namespace CMS.App_Start {
public static class SQLCEEntityFramework {
public static void Start() {
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
// Sets the default database initialization code for working with Sql Server Compact databases
// Uncomment this line and replace CONTEXT_NAME with the name of your DbContext if you are
// using your DbContext to create and manage your database
//DbDatabase.SetInitializer(new CreateCeDatabaseIfNotExists<CONTEXT_NAME>());
}
}
public abstract class SqlCeInitializer<T> : IDatabaseInitializer<T> where T : DbContext {
public abstract void InitializeDatabase(T context);
#region Helpers
/// <summary>
/// Returns a new DbContext with the same SqlCe connection string, but with the |DataDirectory| expanded
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected static DbContext ReplaceSqlCeConnection(DbContext context) {
if (context.Database.Connection is SqlCeConnection) {
SqlCeConnectionStringBuilder builder = new SqlCeConnectionStringBuilder(context.Database.Connection.ConnectionString);
if (!String.IsNullOrWhiteSpace(builder.DataSource)) {
builder.DataSource = ReplaceDataDirectory(builder.DataSource);
return new DbContext(builder.ConnectionString);
}
}
return context;
}
private static string ReplaceDataDirectory(string inputString) {
string str = inputString.Trim();
if (string.IsNullOrEmpty(inputString) || !inputString.StartsWith("|DataDirectory|", StringComparison.InvariantCultureIgnoreCase)) {
return str;
}
string data = AppDomain.CurrentDomain.GetData("DataDirectory") as string;
if (string.IsNullOrEmpty(data)) {
data = AppDomain.CurrentDomain.BaseDirectory ?? Environment.CurrentDirectory;
}
if (string.IsNullOrEmpty(data)) {
data = string.Empty;
}
int length = "|DataDirectory|".Length;
if ((inputString.Length > "|DataDirectory|".Length) && ('\\' == inputString["|DataDirectory|".Length])) {
length++;
}
return Path.Combine(data, inputString.Substring(length));
}
#endregion
}
/// <summary>
/// An implementation of IDatabaseInitializer that will recreate and optionally re-seed the
/// database only if the database does not exist.
/// To seed the database, create a derived class and override the Seed method.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
public class CreateCeDatabaseIfNotExists<TContext> : SqlCeInitializer<TContext> where TContext : DbContext {
#region Strategy implementation
public override void InitializeDatabase(TContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
var replacedContext = ReplaceSqlCeConnection(context);
bool databaseExists;
using (new TransactionScope(TransactionScopeOption.Suppress)) {
databaseExists = replacedContext.Database.Exists();
}
if (databaseExists) {
// If there is no metadata either in the model or in the databaase, then
// we assume that the database matches the model because the common cases for
// these scenarios are database/model first and/or an existing database.
if (!context.Database.CompatibleWithModel(throwIfNoMetadata: false)) {
throw new InvalidOperationException(string.Format("The model backing the '{0}' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.", context.GetType().Name));
}
}
else {
context.Database.Create();
Seed(context);
context.SaveChanges();
}
}
#endregion
#region Seeding methods
/// <summary>
/// A that should be overridden to actually add data to the context for seeding.
/// The default implementation does nothing.
/// </summary>
/// <param name="context">The context to seed.</param>
protected virtual void Seed(TContext context) {
}
#endregion
}
/// <summary>
/// An implementation of IDatabaseInitializer that will <b>DELETE</b>, recreate, and optionally re-seed the
/// database only if the model has changed since the database was created. This is achieved by writing a
/// hash of the store model to the database when it is created and then comparing that hash with one
/// generated from the current model.
/// To seed the database, create a derived class and override the Seed method.
/// </summary>
public class DropCreateCeDatabaseIfModelChanges<TContext> : SqlCeInitializer<TContext> where TContext : DbContext {
#region Strategy implementation
/// <summary>
/// Executes the strategy to initialize the database for the given context.
/// </summary>
/// <param name="context">The context.</param>
public override void InitializeDatabase(TContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
var replacedContext = ReplaceSqlCeConnection(context);
bool databaseExists;
using (new TransactionScope(TransactionScopeOption.Suppress)) {
databaseExists = replacedContext.Database.Exists();
}
if (databaseExists) {
if (context.Database.CompatibleWithModel(throwIfNoMetadata: true)) {
return;
}
replacedContext.Database.Delete();
}
// Database didn't exist or we deleted it, so we now create it again.
context.Database.Create();
Seed(context);
context.SaveChanges();
}
#endregion
#region Seeding methods
/// <summary>
/// A that should be overridden to actually add data to the context for seeding.
/// The default implementation does nothing.
/// </summary>
/// <param name="context">The context to seed.</param>
protected virtual void Seed(TContext context) {
}
#endregion
}
/// <summary>
/// An implementation of IDatabaseInitializer that will always recreate and optionally re-seed the
/// database the first time that a context is used in the app domain.
/// To seed the database, create a derived class and override the Seed method.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
public class DropCreateCeDatabaseAlways<TContext> : SqlCeInitializer<TContext> where TContext : DbContext {
#region Strategy implementation
/// <summary>
/// Executes the strategy to initialize the database for the given context.
/// </summary>
/// <param name="context">The context.</param>
public override void InitializeDatabase(TContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
var replacedContext = ReplaceSqlCeConnection(context);
if (replacedContext.Database.Exists()) {
replacedContext.Database.Delete();
}
context.Database.Create();
Seed(context);
context.SaveChanges();
}
#endregion
#region Seeding methods
/// <summary>
/// A that should be overridden to actually add data to the context for seeding.
/// The default implementation does nothing.
/// </summary>
/// <param name="context">The context to seed.</param>
protected virtual void Seed(TContext context) {
}
#endregion
}
}
I've been using the new MVCScaffolding
for my next CMS and it's been awesome so far. Awesome until I wanted to change my SQLExpress
default database to a SQLCe
local database.
Here ( http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/ ) it says that if I install the "EFCodeFirst.SqlServerCompact
" package it'll take care of changing it and even creating the database file. So I did it.
It created a file called "SQLCEEntityFramework.cs
", in that file there was some errors. Like a reference to "System.Data.Entity.Database
" which does not exist anymore and references to DbDatabase
which is Database
now. So I fixed those errors and ran the application.
Everything is running as usual, but no connection string gets added to my Web.config
and no database file gets created in my App_Data
directory. So now I'm starting to wonder if I'm doing something wrong...
Anybody has any clue of what is happening here and how to fix it?
Thanks a LOT.
EDIT: Just in case you want to see what's in the SQLCEEntityFramework.cs file :
using System;
using System.Data.Entity;
using System.Data.SqlServerCe;
using System.IO;
using System.Transactions;
using System.Data.Entity.Infrastructure;
[assembly: WebActivator.PreApplicationStartMethod(typeof(CMS.App_Start.SQLCEEntityFramework), "Start")]
namespace CMS.App_Start {
public static class SQLCEEntityFramework {
public static void Start() {
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
// Sets the default database initialization code for working with Sql Server Compact databases
// Uncomment this line and replace CONTEXT_NAME with the name of your DbContext if you are
// using your DbContext to create and manage your database
//DbDatabase.SetInitializer(new CreateCeDatabaseIfNotExists<CONTEXT_NAME>());
}
}
public abstract class SqlCeInitializer<T> : IDatabaseInitializer<T> where T : DbContext {
public abstract void InitializeDatabase(T context);
#region Helpers
/// <summary>
/// Returns a new DbContext with the same SqlCe connection string, but with the |DataDirectory| expanded
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected static DbContext ReplaceSqlCeConnection(DbContext context) {
if (context.Database.Connection is SqlCeConnection) {
SqlCeConnectionStringBuilder builder = new SqlCeConnectionStringBuilder(context.Database.Connection.ConnectionString);
if (!String.IsNullOrWhiteSpace(builder.DataSource)) {
builder.DataSource = ReplaceDataDirectory(builder.DataSource);
return new DbContext(builder.ConnectionString);
}
}
return context;
}
private static string ReplaceDataDirectory(string inputString) {
string str = inputString.Trim();
if (string.IsNullOrEmpty(inputString) || !inputString.StartsWith("|DataDirectory|", StringComparison.InvariantCultureIgnoreCase)) {
return str;
}
string data = AppDomain.CurrentDomain.GetData("DataDirectory") as string;
if (string.IsNullOrEmpty(data)) {
data = AppDomain.CurrentDomain.BaseDirectory ?? Environment.CurrentDirectory;
}
if (string.IsNullOrEmpty(data)) {
data = string.Empty;
}
int length = "|DataDirectory|".Length;
if ((inputString.Length > "|DataDirectory|".Length) && ('\\' == inputString["|DataDirectory|".Length])) {
length++;
}
return Path.Combine(data, inputString.Substring(length));
}
#endregion
}
/// <summary>
/// An implementation of IDatabaseInitializer that will recreate and optionally re-seed the
/// database only if the database does not exist.
/// To seed the database, create a derived class and override the Seed method.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
public class CreateCeDatabaseIfNotExists<TContext> : SqlCeInitializer<TContext> where TContext : DbContext {
#region Strategy implementation
public override void InitializeDatabase(TContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
var replacedContext = ReplaceSqlCeConnection(context);
bool databaseExists;
using (new TransactionScope(TransactionScopeOption.Suppress)) {
databaseExists = replacedContext.Database.Exists();
}
if (databaseExists) {
// If there is no metadata either in the model or in the databaase, then
// we assume that the database matches the model because the common cases for
// these scenarios are database/model first and/or an existing database.
if (!context.Database.CompatibleWithModel(throwIfNoMetadata: false)) {
throw new InvalidOperationException(string.Format("The model backing the '{0}' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.", context.GetType().Name));
}
}
else {
context.Database.Create();
Seed(context);
context.SaveChanges();
}
}
#endregion
#region Seeding methods
/// <summary>
/// A that should be overridden to actually add data to the context for seeding.
/// The default implementation does nothing.
/// </summary>
/// <param name="context">The context to seed.</param>
protected virtual void Seed(TContext context) {
}
#endregion
}
/// <summary>
/// An implementation of IDatabaseInitializer that will <b>DELETE</b>, recreate, and optionally re-seed the
/// database only if the model has changed since the database was created. This is achieved by writing a
/// hash of the store model to the database when it is created and then comparing that hash with one
/// generated from the current model.
/// To seed the database, create a derived class and override the Seed method.
/// </summary>
public class DropCreateCeDatabaseIfModelChanges<TContext> : SqlCeInitializer<TContext> where TContext : DbContext {
#region Strategy implementation
/// <summary>
/// Executes the strategy to initialize the database for the given context.
/// </summary>
/// <param name="context">The context.</param>
public override void InitializeDatabase(TContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
var replacedContext = ReplaceSqlCeConnection(context);
bool databaseExists;
using (new TransactionScope(TransactionScopeOption.Suppress)) {
databaseExists = replacedContext.Database.Exists();
}
if (databaseExists) {
if (context.Database.CompatibleWithModel(throwIfNoMetadata: true)) {
return;
}
replacedContext.Database.Delete();
}
// Database didn't exist or we deleted it, so we now create it again.
context.Database.Create();
Seed(context);
context.SaveChanges();
}
#endregion
#region Seeding methods
/// <summary>
/// A that should be overridden to actually add data to the context for seeding.
/// The default implementation does nothing.
/// </summary>
/// <param name="context">The context to seed.</param>
protected virtual void Seed(TContext context) {
}
#endregion
}
/// <summary>
/// An implementation of IDatabaseInitializer that will always recreate and optionally re-seed the
/// database the first time that a context is used in the app domain.
/// To seed the database, create a derived class and override the Seed method.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
public class DropCreateCeDatabaseAlways<TContext> : SqlCeInitializer<TContext> where TContext : DbContext {
#region Strategy implementation
/// <summary>
/// Executes the strategy to initialize the database for the given context.
/// </summary>
/// <param name="context">The context.</param>
public override void InitializeDatabase(TContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
var replacedContext = ReplaceSqlCeConnection(context);
if (replacedContext.Database.Exists()) {
replacedContext.Database.Delete();
}
context.Database.Create();
Seed(context);
context.SaveChanges();
}
#endregion
#region Seeding methods
/// <summary>
/// A that should be overridden to actually add data to the context for seeding.
/// The default implementation does nothing.
/// </summary>
/// <param name="context">The context to seed.</param>
protected virtual void Seed(TContext context) {
}
#endregion
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tom,
我也遇到过 Sql CE 和实体框架的问题。但是,我也许可以解释您所引用的帖子,因为我现在已经熟练地使用它,并且已经通过它奋斗了。
对于初学者来说,该博客条目适用于 MVCScaffolding NuGet 包。不确定您是否知道这个包的作用,但它基本上添加了对 Scott & 的 Scaffolder 的引用。公司创建的目的是在您构建模型后为您动态生成 CRUD。
我的理解是,一旦创建模型、构建项目,您可以在包管理器控制台中运行以下命令,它将创建我上面提到的 CRUD。
现在,此过程完成后,它会在 Models 文件夹下为您的应用程序生成一个 Context 类。如果您在 Start 方法的代码(对于 SQLCEEntityFramework.cs)中看到上面的内容,它提到您需要取消注释该方法的最后一行,以便在数据库不存在时创建该数据库。
最后,一旦执行应用程序,“应该”创建一个 SQL CE 数据库,如果单击 App_Data 文件夹并选择解决方案资源管理器顶部的“显示所有文件”并点击“刷新”图标,您应该会看到您的数据库。
更新:
很抱歉,经过测试,汤姆,你是对的。创建数据库的唯一方法是执行由脚手架创建的视图之一。
Tom,
I too have been experiencing issues with Sql CE and the Entity Framework. BUT, I may be able to explain the post that you are referencing because I am versed in using it now that I have fought my way through it.
For starters, that blog entry was for the MVCScaffolding NuGet package. Not sure if you know what this package does but it basically adds a reference to the Scaffolder that Scott & Company created to dynamically generate CRUD for you once you have built your models.
My understanding is that once you create a model, build your project, you can run the following command in the Package Manager Console and it will create the CRUD that I mentioned above.
Now, once this process is complete, it generates a Context class for your application under the Models folder. If you see above in your code (for SQLCEEntityFramework.cs) in the Start method, it mentions that you need to uncomment the last line of the method in order for it to create the db if it does not exist.
Lastly, once you execute your application, a SQL CE database "should" be created and if you click on the App_Data folder and choose to 'Show All Files' at the top of the Solution Explorer and hit the Refresh icon, you should see your database.
UPDATE:
So sorry, after testing this, Tom, you are correct. Only way the database is created is if you execute one of the views that were created by the Scaffolder.