Reputation: 7489
Is there a way to do, in code, what is done when creating an object context under add New Item --> Entity Data Model in visual studio?
Lets say for instance I add a new table to my database. Next time I load the application that uses a model of that database, it refreshes the model and creates a default conceptual mapping for that new table automatically.
I would probably be using ESQL to query the database rather than LINQ-To-Entities, so I dont care if there is strongly typed access to the new Entity Sets / Objects.
Also, would this solution require EF code first?
Upvotes: 1
Views: 820
Reputation: 364259
My opinion is that EF with EDMX is not ready for this scenarios. Let me clarify why:
ObjectContext
is able to work only with entities provided in these files. DefiningQuery
is just database query - you can use any table present in your database (or even another database) because EF is interested only in result set of this query.
With code first you will have much better development experience. Code first doesn't require EDMX file - it uses mapping defined by code. You will need your application to collect all types derived from EntityTypeConfiguration<>
from predefined (or all referenced) assemblies at bootstrap. All these configurations will be added to DbModelBuilder
in OnModelCreation
when creating the first context instance. You will have a context which can easily use any new entity after restarting application and deploying new assembly with entities. Still you will need code to use this entities. You will have to create DbSets manually instead of exposing them as properties on context class by calling context.Set<EntityType>()
. There are some gotchas when doing this with code first because you must turn off entity model validation and database recreation and you must keep all in sync yourselves.
Still we are discussing scenario where you deploy a new assembly and when application is restarted it will use the assembly. If you require managing tables and "entities" from the application itself then it is not a scenario for direct ORM usage. That is for some metadata models / multi tenant scenarios and it is done completely different way where ORM is used for some general abstract tables and real entity is constructed from metadata stored as abstraction.
Upvotes: 2
Reputation: 3012
Well, the .edmx file is just for visual studio and won't be part of the application. The metadata is stored in three xml files and you can load them at runtime:
You have to map the entities to classes and that is where it becomes ugly. Classes can by generated at runtime with the reflection-api. Or just take a generic class that has many properties like StringProperty1, StringProperty2.
SQL is more appropriate than ESQL for your purpose.
Learning the EF: http://www.testmaster.ch/EntityFramework.test
Upvotes: 0
Reputation: 5645
Why do you actually want to do it this way?
I don't know a way to make this possible. When you create an entity model you actually create an edmx file which is basically xml. Runtime you can't modify these definition files. From this edmx file, T4 templates generate the context and the .NET classes / entities. As far as I know you can't add class definitions to the model / context at runtime.
Maybe this post also can give some more insight EF retrieve entities from undefined table/view. And else let's hope Ladislav Mrinka or Julie Lerman joins this question ;)
About the EF code first. That won't make any difference because the code first approach is also an edmx file / entity model but then generates the database out of it (instead of generating the entity model from the database) the end result is the same and won't work differently runtime.
Upvotes: 0