How to update the Model dynamically?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
sivaprasad.bevara
Posts: 4
Joined: Thu 25 Aug 2011 13:30

How to update the Model dynamically?

Post by sivaprasad.bevara » Fri 04 Nov 2011 14:13

Hi,
I am using the DevArt dot connect for oracle (Version: 6.50.228.0).
I am using the DevArt Entity Model to create the entities.

I have some tables which will be generated on daily basis (Eg:TableName_Date).
All tables will have the same fields.

I need to get the data from the tables when we give the Date. Here I wanted to use only one model to get the data from all the tables by changing the table name in the Model dynamically.

I am able to do this with the code first approach using the the Ado .Net Data Entity model, by recompiling the model in the DbContext class after changing the table name as below.

Code: Select all

DbModelBuilder builder = new DbModelBuilder(DbModelBuilderVersion.V4_1);          
builder.Configurations.Add(new EntityTypeConfiguration());            
builder.Conventions
            .Remove();

 builder.Conventions
            .Remove();

builder.Entity().ToTable(TableName, "TestDatabase");
           
var dbModel = builder.Build(new Devart.Data.Oracle.OracleConnection("connection string"));

var CompiledModel = dbModel.Compile();
But he problem is I am not able use both the "EDML (generated using the DevArt Entity Model)" and "Code First files (created using the Ado .Net Data entity Model)" in the same project.

Do we have any way to update the Model dynamically which is created using the DevArt Entity Model.

Also, I tried to use the Select stored procedure with the DevArt Entity Model but I am facing some problems with this. Can you give please provide some reference to use the "Select" stored procedure with the DevArt Entity Model. In my project I am using Entity Framework 4.1 and DevArt ((Version: 6.50.228.0)

Please help.

Regards,
SivaPrasad.B

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Mon 07 Nov 2011 18:07

Assuming that your task is just to read and display the data from your tables.
We recommend you to implement an approach without using fluent mapping. In your database create a stored procedure (SP) which receives the table name as an IN parameter and returns a cursor (table content) as an OUT parameter. The cursor is filled inside the SP using dynamic SQL. This SP is added to the model, its return value is set to the corresponding entity (if the entity is also added to the model) or to the generated complex type. That's all. Just call the corresponding method of your context, and there is no need to re-create the context.

Assuming that your task is to read/insert/update/delete the data from your tables.
If you are using entity (not complex type) from the previous step, you should also add 3 procedure (insert/update/delete) to your model.

sivaprasad.bevara
Posts: 4
Joined: Thu 25 Aug 2011 13:30

Post by sivaprasad.bevara » Tue 08 Nov 2011 05:55

Hi Shalex,

Thanks for your reply.

I have also tried using the stored procudure that returns a ref cursor.
If you see my last post, I have added comment regrding, using the select stored procedure with the DevArt Entity Model.

I am facing issues while using the select stored procedures. I am using ASP .Net Entity Framework 4.1 and DevArt ((Version: 6.50.228.0) in my application. Please provide some reference site, that explains how we can use the select stored procedures with DevArt Entity Model.

I tried to use the following
http://www.devart.com/blogs/dotconnect/ ... rsors.html

but in this, I didn't find the Entities option, to select the entities for mapping the stored procedure.

Please help.

Regards,
SivaPrasad.B

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Tue 08 Nov 2011 17:42

sivaprasad.bevara wrote:I tried to use the following
http://www.devart.com/blogs/dotconnect/ ... rsors.html

but in this, I didn't find the Entities option, to select the entities for mapping the stored procedure.
Some changes were implemented in Entity Developer after this blog article was written (over 2 years ago). As I understood, you have encountered difficulties in this part of our tutorial: Stored Procedure Returning Result Set using REF CURSOR Out Parameter > Devart Entity Developer. We will correct the article in the following way:
1. The name Project Explorer will be replaced with Model Explorer.
2.
2. Click Entities and then select DEPT from the Entities drop-down list.
-->
2. Click the Value Types drop-down list and then select the DEPT class instead of the GETDEPTPROCResult complex type.
3. Be aware that Entity Developer now can create the method in the CSDL part automatically (no need to drag&drop stored procedure from the Store part in Model Explorer to the designer surface).

Does this help?

sivaprasad.bevara
Posts: 4
Joined: Thu 25 Aug 2011 13:30

Post by sivaprasad.bevara » Mon 14 Nov 2011 10:07

Hi Shalex,

Thanks for your reply.

Sorry for my late response. I still have problems using the stored procedure with DevArt. It is not allowing me to use both DevArt EDMX and ADO .Net Entity Data Model in the same application.

Somehow, I found a way to dynamically change the Table name in the Model.
For this I am using the "DbContext" instead of "EntityObject" for Model Generation Template, while creating the entities using the DevArt.

And in the "Designer.CS" class (which inherits the DbContext class) I have written my function that updates the table in the Model, re-compiles it and returns the context object.

Everything is going well here, but I am facing problem while updating the Entity Model. If I use" update model from the database" option to update the model then, It is creating the Model properly, but the problem is it is recreating the Designer.cs file and it is clearing all my manual updates to the file. Is there any way to keep my changes alive after the Model update?

I have one more thing which is struggling me everytime when I update the Model. That is, I am using the "Domain Services" in my application. In this I didnt find an option to update the Domain Service, when the Entity Model is updated. Now I am creating a new Domain service everytime when I update the Entity Model. Is ther any way we can update the Domain service while updating the Entity Model?

Also what are the pros/cons using EntityObject and DbContext?

Please help.

Regards,
SivaPrasad.B

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Wed 16 Nov 2011 16:30

sivaprasad.bevara wrote:It is not allowing me to use both DevArt EDMX and ADO .Net Entity Data Model in the same application.
Why is it not allowing? Could you please explain?
There is no need to have both *.edmx and *.edml. Please use just *.edml model(-s).
sivaprasad.bevara wrote:Everything is going well here, but I am facing problem while updating the Entity Model. If I use" update model from the database" option to update the model then, It is creating the Model properly, but the problem is it is recreating the Designer.cs file and it is clearing all my manual updates to the file. Is there any way to keep my changes alive after the Model update?
A partial class is generated when using the DbContext template. We recommend you to define your methods in a separate *.cs file (not *.Designer.cs).
sivaprasad.bevara wrote:I have one more thing which is struggling me everytime when I update the Model. That is, I am using the "Domain Services" in my application. In this I didnt find an option to update the Domain Service, when the Entity Model is updated. Now I am creating a new Domain service everytime when I update the Entity Model. Is ther any way we can update the Domain service while updating the Entity Model?
We do not know the way how to refresh the DomainService.cs after updating the corresponding *.edml. Is DomainService.cs updated after "Update From Database" with *.edmx? Please re-create DomainService.cs.
sivaprasad.bevara wrote:Also what are the pros/cons using EntityObject and DbContext?
Please refer to http://stackoverflow.com/questions/5446 ... base-first.

Post Reply