为什么我的实体框架代码第一个DbContext没有显示我填充的表中的实体?

时间:2021-07-23 06:55:34

I am attempting to deploy my app to a testing environment, but cannot get Entity Framework to play nicely with the database. In development, I use a database initializer to seed the database and that has worked flawlessly. However, when I deploy the application to an actual IIS instance I cannot get it to interface with the database. My custom initialization rules aren't run at all, so I am instead creating the database manually.

我正在尝试将我的应用程序部署到测试环境,但无法让Entity Framework与数据库很好地协同工作。在开发过程中,我使用数据库初始化程序为数据库设定种子并且运行良好。但是,当我将应用程序部署到实际的IIS实例时,我无法将其与数据库连接。我的自定义初始化规则根本不运行,因此我手动创建数据库。

I used the ObjectContext.CreateDatabaseScript() as a starting point for my SQL script, and SSMS verifies that two rows are populated in the appropriate table.

我使用ObjectContext.CreateDatabaseScript()作为我的SQL脚本的起点,SSMS验证在相应的表中填充了两行。

My problem arises after running the application. I have custom membership and role providers, neither of which seem to detect that the two roles exist in the database.

运行应用程序后出现问题。我有自定义成员资格和角色提供程序,它们似乎都没有检测到数据库中存在这两个角色。

How do I get my entity framework to recognize that these rows aren't empty? I'm currently using a private DbContext inside a repository to handle communication with Entity Framework, and disabled my custom initializer until this hiccup is resolved.

如何让我的实体框架识别出这些行不为空?我目前在存储库中使用私有DbContext来处理与Entity Framework的通信,并禁用我的自定义初始化程序,直到解决此打嗝。

Code that attempts to find roles in the database:

尝试在数据库中查找角色的代码:

context.Roles.Single(r => r.Name == role)

The database shows the following in the Roles table:

数据库在Roles表中显示以下内容:

Id  Name     Description
1   Company  NULL
2   Customer NULL

The LINQ generates an empty sequence exception, as context.Roles is empty.

LINQ生成一个空序列异常,因为context.Roles为空。

2 个解决方案

#1


0  

Judging by the comments it looks like there is no connection between Entity Framework and the database. You don't mention which version of Entity Framework you are using but I assume you have updated to the latest (4.3 as of time of writing).

从评论来看,实体框架和数据库之间没有任何联系。您没有提到您正在使用的实体框架版本,但我认为您已更新到最新版本(截至撰写本文时为4.3)。

I notice you say that you are "creating the database manually.". Why not open a test project and create a new database first model based on your manually created database? This should at least confirm that you can use Entity Framework with your configuration. From there I would create another project from scratch to test the Code First approach.

我注意到你说你是“手动创建数据库”。为什么不打开测试项目并根据手动创建的数据库创建新的数据库第一个模型?这至少应该确认您可以将Entity Framework与您的配置一起使用。从那里我将从头开始创建另一个项目来测试Code First方法。

#2


0  

Here are the steps I went through to migrate from a code first project with a custom IDatabaseInitializer implementation that reset the database during each session to a non-volatile DB setup.

以下是我从代码优先项目迁移到自定义IDatabaseInitializer实现的步骤,该实现在每个会话期间将数据库重置为非易失性数据库设置。

First, copy the schema generated by Entity Framework in your dev. project by running the following code and placing the output in an accessible location (ie: file on desktop, raw text in browser, etc). context is an instance of your class that inherits from DbContext:

首先,在您的dev中复制Entity Framework生成的模式。通过运行以下代码并将输出放在可访问的位置(即:桌面上的文件,浏览器中的原始文本等)来进行项目。 context是继承自DbContext的类的实例:

((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript()

Second, save the string returned from that call in a SQL file. Be sure to remove the command that creates the Entity Framework meta data table. This command should resemble the following:

其次,将该调用返回的字符串保存在SQL文件中。请务必删除创建Entity Framework元数据表的命令。此命令应类似于以下内容:

create table [dbo].[EdmMetadata] 
( 
    [Id] [int] not null identity, 
    [ModelHash] [nvarchar](max) null, 
    primary key ([Id]) 
);

Next, remove the code that seeds the database. I used an AppSetting so that I can easily toggle that code's usage after deployment without needing to re-compile and deploy.

接下来,删除种子数据库的代码。我使用了AppSetting,这样我就可以在部署后轻松切换代码的​​使用,而无需重新编译和部署。

if (ConfigurationManager.AppSettings["Mode"] == "Dev")
{
    Database.SetInitializer<PonosContext>(new PonosInitializer());
    new MyContext().Database.Initialize(true);
}

Immediately outside of that statement, you still need to intialize the database, but make sure to pass in false so that the initializing will only happen if the database has not been initialized.

紧接在该语句之外,您仍然需要初始化数据库,但确保传入false,以便只有在数据库尚未初始化时才会进行初始化。

new MyContext().Database.Initialize(false);

Finally, run your setup SQL on an empty database to have the tables created with appropriate fields.

最后,在空数据库上运行安装SQL,以使用适当的字段创建表。

If you deploy and then run your application, it should connect to the database and be able to work as normal with any data you loaded in an external script. I tested this method successfully with custom membership and role providers.

如果部署然后运行应用程序,它应该连接到数据库,并且能够正常使用您在外部脚本中加载的任何数据。我用自定义成员资格和角色提供程序成功测试了此方法

#1


0  

Judging by the comments it looks like there is no connection between Entity Framework and the database. You don't mention which version of Entity Framework you are using but I assume you have updated to the latest (4.3 as of time of writing).

从评论来看,实体框架和数据库之间没有任何联系。您没有提到您正在使用的实体框架版本,但我认为您已更新到最新版本(截至撰写本文时为4.3)。

I notice you say that you are "creating the database manually.". Why not open a test project and create a new database first model based on your manually created database? This should at least confirm that you can use Entity Framework with your configuration. From there I would create another project from scratch to test the Code First approach.

我注意到你说你是“手动创建数据库”。为什么不打开测试项目并根据手动创建的数据库创建新的数据库第一个模型?这至少应该确认您可以将Entity Framework与您的配置一起使用。从那里我将从头开始创建另一个项目来测试Code First方法。

#2


0  

Here are the steps I went through to migrate from a code first project with a custom IDatabaseInitializer implementation that reset the database during each session to a non-volatile DB setup.

以下是我从代码优先项目迁移到自定义IDatabaseInitializer实现的步骤,该实现在每个会话期间将数据库重置为非易失性数据库设置。

First, copy the schema generated by Entity Framework in your dev. project by running the following code and placing the output in an accessible location (ie: file on desktop, raw text in browser, etc). context is an instance of your class that inherits from DbContext:

首先,在您的dev中复制Entity Framework生成的模式。通过运行以下代码并将输出放在可访问的位置(即:桌面上的文件,浏览器中的原始文本等)来进行项目。 context是继承自DbContext的类的实例:

((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript()

Second, save the string returned from that call in a SQL file. Be sure to remove the command that creates the Entity Framework meta data table. This command should resemble the following:

其次,将该调用返回的字符串保存在SQL文件中。请务必删除创建Entity Framework元数据表的命令。此命令应类似于以下内容:

create table [dbo].[EdmMetadata] 
( 
    [Id] [int] not null identity, 
    [ModelHash] [nvarchar](max) null, 
    primary key ([Id]) 
);

Next, remove the code that seeds the database. I used an AppSetting so that I can easily toggle that code's usage after deployment without needing to re-compile and deploy.

接下来,删除种子数据库的代码。我使用了AppSetting,这样我就可以在部署后轻松切换代码的​​使用,而无需重新编译和部署。

if (ConfigurationManager.AppSettings["Mode"] == "Dev")
{
    Database.SetInitializer<PonosContext>(new PonosInitializer());
    new MyContext().Database.Initialize(true);
}

Immediately outside of that statement, you still need to intialize the database, but make sure to pass in false so that the initializing will only happen if the database has not been initialized.

紧接在该语句之外,您仍然需要初始化数据库,但确保传入false,以便只有在数据库尚未初始化时才会进行初始化。

new MyContext().Database.Initialize(false);

Finally, run your setup SQL on an empty database to have the tables created with appropriate fields.

最后,在空数据库上运行安装SQL,以使用适当的字段创建表。

If you deploy and then run your application, it should connect to the database and be able to work as normal with any data you loaded in an external script. I tested this method successfully with custom membership and role providers.

如果部署然后运行应用程序,它应该连接到数据库,并且能够正常使用您在外部脚本中加载的任何数据。我用自定义成员资格和角色提供程序成功测试了此方法