实体框架,POCO,通过代码创建自己的DB?
我很想知道是否有任何合理的方法来创建自己的数据库,CREATE MyDatabase
,CREATE Table1
在实体DbContext
内,mabye InitializeDatabase
方法? 我知道带有 POCO 的 EF 4.1 会为我做到这一点,但现在看来,如果我想在数据库初始化期间生成一些事件(请参阅编辑),我可能想在我的代码中生成数据库。
假设我想向最终用户报告数据库创建进度,我需要在代码中创建数据库蚂蚁表,为此,我需要弄清楚如何在代码中创建数据库(而不是表)。
我已经尝试过:
public void InitializeDatabase(MyContext context)
{
context.Database.ExecuteSqlCommand("Query to create table");
}
但问题是我需要先创建数据库才能创建表,但如何做到这一点?或者有更好的方法来做我想做的事?
编辑:
是的,我知道 POCO 会自动为我执行此操作,但不幸的是,似乎我确实需要/想要通过我的代码使用 POCO 创建数据库和表,原因是我想显示进度在数据库创建/初始化短语期间?我能出来的唯一方法是通过我自己的代码创建数据库并创建表,并在创建每个表后生成事件。
I am interested to know is there are any reasonable ways to create your own DB, CREATE MyDatabase
, CREATE Table1
inside Entity DbContext
, mabye InitializeDatabase
method?
I know EF 4.1 with POCO will do it for me, but now seems that if I want generate some events during DB initialize(see edit), I probably want generate DB in my code.
Say if I want to report the DB creation progress to the end user, I need create Database ant Tables in my code, in order to to that, I need figure out how to to create Database(not table) inside my code.
I've tried:
public void InitializeDatabase(MyContext context)
{
context.Database.ExecuteSqlCommand("Query to create table");
}
But the problem is that I need create the Database first in order to create tables, but how to do that? Or there are better ways to do what I want?
EDIT:
Yes, I know POCO will do that for me automatically, but unfortunately, seems that I do need/want create Database and Tables by my code with POCO, the reason is that I want show the progress during DB creation/initialize phrase? The only way I can come out is to create Database and create tables though my own code and generate events after each table been created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实体框架将尝试创建您在连接字符串中配置的数据库。只需确保连接到服务器的用户有权创建数据库即可。
Entity Framework will attempt to create the database you configured in the connection string. Just make sure that the user that connects to the server has rights to create a database.
您正在做的事情看起来像实体框架代码优先。
如果需要,您可以在模型生成器
Seed
方法中创建数据库和/表。如果您的唯一目标是向用户报告进度,则无需使用自己的查询创建数据库。您可以使用
ModelBuilder
OnModelCreating
来完成此操作。What you are doing looks like Entity Framework Code First.
You can create a Database and / Tables in the Model Builder
Seed
method if you want.If your only goal is to report to the user the progress, you don't need to create the database using your own queries. You can do this using the
ModelBuilder
OnModelCreating
.!至少,如果您使用的是 Entity Framework 4(该版本随 .Net 4.0 一起提供,需要 Visual Studio 2010。VS 2008 无法使用 .Net 4.0),则不会。。
这称为代码优先开发。基本上,您编写数据类,实体框架会根据类关系、属性名称和一些可选的 EF 特定属性自动推断数据库结构。然后它会自动为您创建数据库(尽管对自动迁移的支持很少......for现在)。
关于 Code First 有一些简单的教程 此处和此处。
Nope! At least, not if you're using Entity Framework 4 (The version shipped with .Net 4.0, which requires Visual Studio 2010. VS 2008 cannot use .Net 4.0).
It's called Code-First development. Basically, you write your data classes, and Entity Framework infers the database structure automatically based on the class relationships, property-names, and some optional EF-specific attributes. It then creates the database for you automatically (though support for automatic migrations is minimal... for now).
There are some simple tutorials on Code First here and here.