获取模型架构,以使用不支持CreateDatabase的提供程序以编程方式创建数据库 [英] Get Model schema to programmatically create database using a provider that doesn't support CreateDatabase

查看:78
本文介绍了获取模型架构,以使用不支持CreateDatabase的提供程序以编程方式创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用用于实体框架5的SQLite提供程序,但它不支持CreateDatabase,因此无法自动创建数据库。 (代码优先)

I'm using the SQLite provider for Entity Framework 5 but it doesn't support CreateDatabase and thus cannot auto create the database. (Code First)

是否可以在运行时获取Model模式,以便自己创建SQL CREATE TABLE命令?

Is there a way I can obtain the Model schema at runtime so that I can create the SQL "CREATE TABLE" command myself?

如果不是在运行时,还有其他方式来获取架构,这样我就知道如何正确创建表了。

If not at runtime, some other way to obtain the schema so I know how to create the table properly?

谢谢!

推荐答案

A)至于在运行时获取模型架构部分

A) As for obtaining the model schema at runtime part

(都是我的早期职位)

查看此内容如何以编程方式读取EF DbContext元数据?

如何通过单元测试检查属性标记为在ORM模型中计算出来的?

这也是一个自定义项初始化器EF5代码优先迁移中的程序化数据转换

Also this one for a custom initializer Programmatic data transformation in EF5 Code First migration

话虽如此...

我看到的问题是您实际上在哪里以及在什么时候有可用的数据

实际上,我很确定您将无法在任何时间进行操作。

因为能够提取该信息您需要运行 DbContext -因此必须构造db等。

The problem I see is where and at what point you actually have the data available.
Actually I'm quite sure you won't be able to do that at any time.
Because to be able to extract that info you need to have a DbContext running - so db has to be constructed etc. etc.

在初始化器中-但是使用不同的方式获取该信息-以上内容不可用。

In the initializer maybe - but using different ways to get that info - the above is not available.

B)
走实现提供者,生成者等的方式(例如这篇文章)。

应该在正确的时间从EF / CF本身获取所有信息。

但是我玩的还不多。

B) The other way would be to go the way of implementing a provider, generator etc. (e.g. this post).
That way you should get all that info just at the right time from the EF/CF itself.
However I haven't played with that much.

有关更多信息,请查看 EF源代码

For more info you can check the EF source code

到目前为止,这更像是一个收集的信息,以防它帮助您找到任何地方。并不是真正的解决方案。明天我会再添加一些。

This is more of a 'gathered info' so far - in case it helps you get anywhere with it. Not really a solution. I'll add some more tomorrow.

编辑:

真实的数据库元数据,请查看其他 DataSpace ,这应该可以将您带到正确的位置...

(注意:事情往往会得到不太精确-显然没有适当的官方支持)

To get the real database metadata, look into the other DataSpace, this should get you to the right place...
(note: things tend to get less exact from here - as obviously there isn't the right official support)

var ssSpaceSet = objectContext.MetadataWorkspace.GetItems<EntityContainer>(DataSpace.SSpace).First()
    .BaseEntitySets
    .First(meta => meta.ElementType.Name == "YourTableName");

如果在调试器中查找,则 Table 属性应该具有真实的表名。

但是,可能需要 reflection

If you look up in debugger, Table property should have the real table name.
However, reflection might be required.

如何以编程方式读取EF DbContext元数据?

如何通过单元测试来检查属性标记是否为ORM模型中计算出的结果?

在EF5代码首次迁移

实体框架MigrationSqlGenerator用于SQLite

http://entityframework.codeplex.com/

实体框架-从实体获取表名

首先使用ef代码:获取没有数据注释的实体表名称

从实体框架元数据中获取数据库表名称

http ://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

这篇关于获取模型架构,以使用不支持CreateDatabase的提供程序以编程方式创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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