2

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)

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?

Thanks!

Josh Mackey
  • 233
  • 2
  • 10

1 Answers1

4

A) As for obtaining the model schema at runtime part

(all are earlier posts of mine)

See this one How I can read EF DbContext metadata programmatically?

And How check by unit test that properties mark as computed in ORM model?

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

Having said that...

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) 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.

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.

EDIT:

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");

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


How I can read EF DbContext metadata programmatically?
How check by unit test that properties mark as computed in ORM model?
Programmatic data transformation in EF5 Code First migration
Entity Framework MigrationSqlGenerator for SQLite
http://entityframework.codeplex.com/
Entity Framework - Get Table name from the Entity
ef code first: get entity table name without dataannotations
Get Database Table Name from Entity Framework MetaData
http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • Damn, that's nasty but it does appear to have what I need (your part A). As it turns out, you CAN access this information without a database running. I can get it all in the DatabaseInitializer. I'll accept this answer for the time being unless it turns out it won't work. – Josh Mackey Apr 09 '13 at 01:57
  • Problem found! Obtaining the metadata through the DbContext doesn't return the Table name if set via attributes or the fluent api. – Josh Mackey Apr 09 '13 at 02:56
  • it's in the different `space` - I just edited in. The only problem is you'd have to use reflection to get there (Table property is `internal`) - but maybe it is exposed somewhere directly. – NSGaga-mostly-inactive Apr 09 '13 at 11:11
  • there is another option in that code project article (end) - which doesn't / shouldn't load the cf at all, not exact science again, but interesting hack – NSGaga-mostly-inactive Apr 09 '13 at 12:04
  • Thanks, I'll take a look. At this point, interestingly enough, it might just be easiest to modify SQLite and do it properly then submit the patch back for inclusion... – Josh Mackey Apr 10 '13 at 22:34