代码首先与现有的空数据库 [英] Code-First with existing empty database

查看:122
本文介绍了代码首先与现有的空数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,我将要使用Entity Framework 4.1。我正在计划做代码优先的方法,因为我喜欢干净的实体,我想以某种方式扩展它们。



我有一个现有的空数据库,我想使用。我已经设置了如下的连接字符串,我设置了EF总是删除和创建数据库(开始)。这是我的问题。



我可以访问这个空的数据库,但是我没有删除它和/或创建它的权限。数据库已经可以使用了。如何让EF使用数据库,而不是删除和创建它?



顺便说一句,如果我没有设置它删除和创建,它抱怨说它找不到表,这是可以理解的。

解决方案

为了使用EF Code First空数据库,您将需要使用自定义数据库初始化程序,不重新创建数据库,但只有表,幸运的是有人这样做:



http:// blogs .microsoft.co.il / blogs / gilf / archive / 2011/05/30 / creation-a-code-first-database-initializer-strategy.aspx



您还应该看看EF 4.3的新数据库迁移功能,这在您的架构演变过程中变得非常重要。


I am doing a project, where I am going to use Entity Framework 4.1. I am planning on doing the "Code-First" approach, as I like clean entities and I want to extend them in some fashion.

I have an existing empty database, that I would like to use. I have setup the connection string as below, and I have set EF to always drop and create database (to start with). And this is my problem.

I have access to this empty database, but I don't have permissions to drop it and/or create it. The database is already there ready to use. How can I get EF to use the database as it is, instead of dropping and creating it?

By the way, if I doesn't set it to drop and create, it complains about that it cannot find the tables, which is understandable.

解决方案

In order to use EF Code First with an existing empty DB you will need to use a custom database initializer, that does not recreate the DB but only the tables, luckily somebody did this already:

http://blogs.microsoft.co.il/blogs/gilf/archive/2011/05/30/creating-a-code-first-database-initializer-strategy.aspx

You should also have a look at the new Database Migration functionality of EF 4.3 this becomes important as your schema evolves.

这篇关于代码首先与现有的空数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆