简体   繁体   English

外部SQL服务器上的实体框架代码优先

[英]Entity Framework Code First on external SQL server

I'm following the Entity Framework tutorial on: Link 我在关注Entity Framework教程: 链接

I've downloaded the source code, and ran. 我已经下载了源代码,然后运行了。 The project works fine (using the default connection string). 该项目工作正常(使用默认的连接字符串)。

<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0" />

Next i've changed the connection string to connect to a remote server (which successfully connects). 接下来,我更改了连接字符串以连接到远程服务器(成功连接)。 However the tables aren't created and when running the application and accessing the controller I get the following error. 但是未创建表,并且在运行应用程序并访问控制器时出现以下错误。

Error: 错误:

Model compatibility cannot be checked because the database does not contain 
model metadata. Model compatibility can only be checked for databases created 
using Code First or Code First Migrations.

My database user is 'dbowner' so I wouldn't imagine it's database access issues. 我的数据库用户是“ dbowner”,所以我无法想象这是数据库访问问题。

I'm new to EF, and don't know much about Code First Migrations. 我是EF的新手,对Code First迁移了解不多。 Have you come across the above error, and would Code Migrations solve this issue? 您是否遇到上述错误,代码迁移会解决此问题吗? If so why? 如果可以,为什么?

From my reading (please correct me if I am wrong) the scenario here is that you have an existing (perhaps empty) database on a remote server that you wish to put your EF code-first work into. 根据我的阅读(如果我错了,请纠正我),这里的情况是您希望将EF代码优先的工作放到远程服务器上,现有数据库(可能是空的)。

The error is coming about because, I think, EF is looking for a _MigrationHistory table (metadata about what EF code-first migrations have been run against it etc) and can't find it. 错误即将到来是因为,我认为EF正在寻找_MigrationHistory表(有关已针对它运行了EF代码优先迁移的元数据),并且找不到它。 There is some reasonable coverage of this table here for some background/interest reading. 此处有一些合理的表格覆盖范围,供您阅读一些背景知识。

So to resolve the issue, I think the steps to take are: 因此,要解决此问题,我认为要采取的步骤是:

  1. Initialise your database so that it acknowledges the EF code-first stuff 初始化您的数据库,使其确认EF代码优先
  2. Update the database, rolling forward all your migrations to date 更新数据库,前滚所有迄今为止的迁移

I found some good coverage of how to do this here . 我在这里找到了一些很好的方法。 This blog also has some good coverage of EF topics in general 该博客还对EF主题进行了很好的介绍

PS. PS。 I am guessing that your .dsf/SQL Compact Edition database wouldn't have had this problem because EF code-first would have created it for you on first run so it was already acknowledged as being a code-first database. 我猜想您的.dsf / SQL Compact Edition数据库不会出现此问题,因为EF代码优先会在首次运行时为您创建它,因此它已经被公认是代码优先数据库。

Here is a link to Entity Framework Power Tools. 是Entity Framework Power Tools的链接。 It is made for creating models by 'Reverse Engineering' your Database on a remote server. 它用于通过在远程服务器上对数据库进行“反向工程”来创建模型。 You can then easily access this database using EF. 然后,您可以使用EF轻松访问该数据库。

Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database 反向工程师代码优先-为现有数据库生成POCO类,派生的DbContext和代码优先映射

Both of the initializer methods which I had tried fail when the database already exists: 当数据库已经存在时,我尝试过的两个初始化方法都失败了:

Database.SetInitializer<Context>(new Initializer());
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Context>());

However it is possible to force the database to be dropped using: 但是,可以使用以下方法强制删除数据库:

Database.SetInitializer(new DropCreateDatabaseAlways<Context>());

The following SO post provides the answer to my question: Setting up a Entity Framework Code First Database on SQL Server 2008 以下SO帖子提供了我的问题的答案: 在SQL Server 2008上设置实体框架代码优先数据库

I've tried a combination of the two, and have decided that the best solution is to manually go into SQL Management studio and DROP the database, and re-create it using the initializer as this allows me to Seed the contents of the database. 我已经尝试将两者结合起来,并决定最好的解决方案是手动进入SQL Management Studio并删除数据库,然后使用初始化程序重新创建它,因为这使我可以播种数据库的内容。

Database.SetInitializer<Context>(new Initializer());

See here for more information on Seeding the database as it is also quite an unstable processess! 请参阅此处,以获取更多有关播种数据库的信息,因为它也是一个不稳定的过程! How can I get my database to seed using Entity Framework CodeFirst? 如何使用Entity Framework CodeFirst将数据库植入种子?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM