2 Replies - 8421 Views - Last Post: 06 July 2011 - 02:13 AM

#1 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Entity Framework Model Change At Runtime

Posted 05 July 2011 - 03:28 AM

I'm not even sure where to put this question as it doesn't seem to fit any one category. Development is done in visual studio 2010, language is vb.net and we're using oracle databases as well as sql ce databases. Anyway, here's my problem:

I've go an oracle database which I am accessing via the entity framework. Works just fine. Now, at runtime, several SQL CE databases are added. All of these have identical tables with different contents.

edit: It seems there is no real way to add multiple databases to my EF model, let alone do that at run time. Correct me if I'm wrong, though.

This post has been edited by Nightfish: 05 July 2011 - 04:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Entity Framework Model Change At Runtime

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Entity Framework Model Change At Runtime

Posted 05 July 2011 - 08:14 AM

There is no supported way to create a EF model at run-time. However, that doesn't mean it's impossible. If you look at what the EF model is, it's simply strongly typed classes and XML. You can create the strongly typed classes on the fly(you know the DB structure, right?) and modify the XML in the .edmx using LINQ-to-XML or XPath(or the like).

So is it possible? Sure. Will it be easy? no.

I did find this though..

http://blog.bennymic...del-at-runtime/
Was This Post Helpful? 0
  • +
  • -

#3 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Entity Framework Model Change At Runtime

Posted 06 July 2011 - 02:13 AM

Solved it:

        Dim filePath = "D:\data\Test_DB1.sdf"
        Dim sqlCeConnectionString = String.Format("Data Source={0}", filePath)

        'Create an EDM connection
        Dim builder = New EntityConnectionStringBuilder()
        builder.Metadata = "res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl"
        builder.Provider = "System.Data.SqlServerCe.3.5"
        builder.ProviderConnectionString = sqlCeConnectionString

        Dim edmConnectionString = builder.ToString()
        Dim edmConnection = New EntityConnection(edmConnectionString)

        Dim myData = New Test_DB1Entities(edmConnection)
        Dim queryData = From x In myData.PERSON Select x.PERSON_NAME




All I need to do now is change the filePath and I can swap and add databases to my heart's content.

This post has been edited by Nightfish: 06 July 2011 - 05:03 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1