简体   繁体   English

为什么我的实体框架代码不首先DbContext显示我填充的表中的实体?

[英]Why doesn't my Entity Framework code first DbContext show the entities from my populated table?

I am attempting to deploy my app to a testing environment, but cannot get Entity Framework to play nicely with the database. 我正在尝试将我的应用程序部署到测试环境,但无法让Entity Framework与数据库很好地协作。 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. 但是,当我将应用程序部署到实际的IIS实例时,我无法将其与数据库连接。 My custom initialization rules aren't run at all, so I am instead creating the database manually. 我的自定义初始化规则根本不运行,所以我改为手动创建数据库。

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为空。

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. 这至少应该确认您可以在配置中使用Entity Framework。 From there I would create another project from scratch to test the Code First approach. 从那里我将从头开始创建另一个项目来测试Code First方法。

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. 首先,在您的dev中复制Entity Framework生成的模式。 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 : context是继承自DbContext的类的实例:

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

Second, save the string returned from that call in a SQL file. 其次,将该调用返回的字符串保存在SQL文件中。 Be sure to remove the command that creates the Entity Framework meta data table. 请务必删除创建Entity Framework元数据表的命令。 This command should resemble the following: 此命令应类似于以下内容:

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. 我用自定义成员资格和角色提供程序成功测试了此方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能使用Entity Framework Code First将新数据添加到数据库中? - Why can't I add new data to my database with Entity Framework Code First? 首先是实体框架代码:为什么不创建我的收藏集? - Entity framework code first: why is my collection not created? 为什么实体框架代码首先更改我的类型名称? - Why Does Entity Framework Code First Change my Type Names? 为什么Entity Framework 5代码优先不映射我的枚举属性? - Why is Entity Framework 5 code-first not mapping my enum property? 我的数据库不会先使用实体​​框架代码进行播种 - my database won't seed using entity framework code first 在Entity Framework中使用代码优先dbcontext进行选择时出现EntitySqlException - EntitySqlException on select with code first dbcontext in Entity Framework 在Code第一实体框架中将DbContext转换为Datatable - Convert DbContext to Datatable in Code first entity framework 表拆分实体框架代码优先-3个以上实体 - Table Splitting Entity Framework Code First - 3+ Entities 首先使用Entity Framework代码将2个相关实体合并到1个表中 - 2 related entities into 1 table using Entity Framework code first 我的 Context 没有从 Entity Framework Core 中的 DbContext 继承 - My Context does not inherit from DbContext in Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM