EF 4.1 Code First 和现有数据库以及 .NET 成员资格

发布于 2024-10-27 12:53:58 字数 455 浏览 1 评论 0原文

我的开发盒上有一个名为 ApplicationName_Development 的数据库在 SQL Server 2008 R2 Developer 版本上运行。

我将 .NET 成员资格表添加到数据库中没有任何问题。当我尝试让 Code First 工作时,我收到以下错误消息:

服务器遇到错误 处理请求。例外情况 消息是“型号兼容性不能 被检查,因为数据库确实 不包含模型元数据。确保 IncludeMetadataConvention 有 已添加到 DbModelBuilder 约定。

经过一番谷歌搜索后,我发现我必须删除数据库并让 EF 创建数据库。没关系,但我丢失了所有 .NET 成员资格表。我可以返回并再次添加成员资格表,但如果我的模型发生更改并且 EF 需要重新创建数据库,那么我必须再次添加成员资格表。

我该如何解决这个问题?

I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box.

I added .NET membership tables to the database with no problem. When I tried to get Code First working I received the following error message:

The server encountered an error
processing the request. The exception
message is "Model compatibility cannot
be checked because the database does
not contain model metadata. Ensure
that IncludeMetadataConvention has
been added to the DbModelBuilder
conventions.

After some googling, I discovered that I had to delete the database and let EF create the database. That's fine but I lost all my .NET membership tables. I can go back in and add the membership tables again but if my model changes and EF needs to recreate the database then I have to add the membership tables in again.

How do I get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

禾厶谷欠 2024-11-03 12:53:58

这就是代码优先的工作原理。代码优先的主要思想是您不接触数据库,因为创建数据库是模型的责任。如果您想自定义数据库,则必须创建自定义 IDatabaseInitializer 并添加自定义 SQL。

public class MyDbInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
    protected override void Seed(MyContext context)
    {
        // Here run your custom SQL commands
        context.Database.ExecuteSqlCommand("CREATE TABLE ....");
    }
}

现在,您只需要在应用程序启动时设置自定义初始化程序:

Database.SetInitializer<MyContext>(new MyDbInitializer());

如果您不想这样做,则必须手动维护数据库并将初始化程序设置为空。

This is how code-first work. Main idea of code first is that you do not touch your database because it is responsibility of the model to create the database. If you want to customize your database you must create custom IDatabaseInitializer and add your custom SQL.

public class MyDbInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
    protected override void Seed(MyContext context)
    {
        // Here run your custom SQL commands
        context.Database.ExecuteSqlCommand("CREATE TABLE ....");
    }
}

Now you only need setup your cutom intializer on the startup of your application:

Database.SetInitializer<MyContext>(new MyDbInitializer());

If you don't want to do it this way you must manually maintain your database and set initializer to null.

做个ˇ局外人 2024-11-03 12:53:58

在这里找到了更简单的解决方法。我希望这有帮助。

http://www.paragm。 com/ef-v4-1-code-first-and-asp-net-membership-service/

Found a easier workaround here. I hope this helps.

http://www.paragm.com/ef-v4-1-code-first-and-asp-net-membership-service/

梦里兽 2024-11-03 12:53:58

另一种选择是使用 System.Web.Management 命名空间。我使用下面的代码取得了巨大的成功:

string connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseContext"].ConnectionString;
string database = "MyDatabaseName";
SqlServices.Install(database, SqlFeatures.All, connectionString);

它只会创建数据库,然后您可以使用标准会员资格 API 添加用户。

Another option could be to use the System.Web.Management namespace. I've had great success with the code below:

string connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseContext"].ConnectionString;
string database = "MyDatabaseName";
SqlServices.Install(database, SqlFeatures.All, connectionString);

It will just create the database and after that you can add users with the standard membership API.

心凉 2024-11-03 12:53:58

这是另一种可能性。

如果您查看 MvcMusicStore 示例,就会发现有一个 SampleData 类负责在重建时为数据库播种。 SampleData 类继承自 DropCreateDatabaseIfModelChanges,并重写 Seed 方法。该类被传递给 global.asaxApplication_Start 方法中的 Database.SetInitializer

在我将 SampleData 的父类更改为 CreateDatabaseIfNotExist 之前,我遇到了与您相同的错误。

然后,您可以重写 Seed 方法以在启动时插入所需的任何数据,而不会破坏数据库。

Here's another possibility.

If you look at the MvcMusicStore sample - there's a SampleData class that is responsible for seeding the database on a rebuild. The SampleData class inherits from DropCreateDatabaseIfModelChanges, and overrides the Seed method. This class is passed to the Database.SetInitializer in the Application_Start method in global.asax.

I was getting the same error as you until I changed the parent class of SampleData to CreateDatabaseIfNotExist.

Then you can override the Seed method to insert any data you desire at startup, without it blowing away the database.

阳光下慵懒的猫 2024-11-03 12:53:58

在开发时,创建 2 个数据库和两个连接字符串。一种用于 SqlMembership(使用 aspnet_regsql),另一种用于 EF 应用程序。如果您想在生产中将它们合并到单个数据库中,只需将 web.config.release 中的连接字符串更改为相同即可。然后,EF 模型更改只会删除您的应用程序数据库,而不是您的会员数据库。

通过单独处理身份验证组件,您自然会将身份验证系统与应用程序系统解耦。然后,如果您想更换会员提供商,您将得到更好的设置。

随着系统的发展,您可能需要首先支持没有 EF 代码的非纯模型,因此这是沿着这条道路走下去的一个很好的模板。

While you are developing, create 2 databases and two connection strings. One for SqlMembership (using aspnet_regsql) and one for your EF Application. If you would like to merge them into a single DB in production, just change the connection string in web.config.release to be the same. Then, EF model changes will just drop your apps db and not your membership DB.

By treating your authentication component separately, you will naturally decouple your authentication system from your application system. Then, if you wish to change membership providers, you will be better setup.

As the system grows, you will likely need to support non-pure models without EF code first, so this is a good template for going down that path.

沐歌 2024-11-03 12:53:58

我发现不玩其他任何东西的最简单方法如下。

我第一次运行应用程序时始终在 Initilizer 中使用 DropAndRecreatedatabase

这是我第一次创建数据库。

接下来,我将其更改为 DropCreateDatabaseIfModelChanges

I found the easiest way without playing with anything else was the following.

I ran the application first time with DropAndRecreatedatabase always in the Initilizer.

This created my database for the first time.

Following this I changed this to DropCreateDatabaseIfModelChanges.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文