in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Creating a Entity Framework Connection in a DLL project

Last post 08-23-2009 1:41 AM by rBg. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 08-18-2009 12:22 PM

    • GuZ
    • Not Ranked
    • Joined on 07-19-2009
    • Posts 2

    Creating a Entity Framework Connection in a DLL project

    Hi,

    I'm developing a library Project as a plugin for another program and have similar question like the one posted in  this post:

     http://sqlite.phxsoftware.com/forums/t/1895.aspx

    But unlike in that post I cannot access the app.config as the program won't grant me access to it anyway (and certainly does not even use an app.config). Another issue in this scope is that .Net libraries normally do not support having own app.configs.

    So basically I'm not in the position to use the suggested app.config settings. So

    1. Can I programmatically remove and replace the invariant System.Data.SQLite namepsace entry for the SQLiteFactory used by the Entity Framework?

    2. (If 1 is yes,) how would a programmatical approach look like basically?

    3. Is there another way of sneaking the data provider into the knownlege of .Net e.g. by adding the entry into the machine.config? I consider this as an option because it is mentioned in the "DbProviderFactories and You" section in the help file.

    Filed under: ,
  • 08-23-2009 1:41 AM In reply to

    • rBg
    • Not Ranked
    • Joined on 08-23-2009
    • Posts 4

    Re: Creating a Entity Framework Connection in a DLL project

    Ahhh, I ran into the same problem, and I actually solved it by doing this.  In a module, I pass the dbLocation and dbPassowrd..

     

        Public _db As yourEnt


        Private _dbLocation As String
        Public Property dbLocation() As String
            Get
                Return _dbLocation
            End Get
            Set(ByVal value As String)
                _dbLocation = value
            End Set
        End Property

        Private _dbPassword As String
        Public Property dbPassword() As String
            Get
                Return _dbPassword
            End Get
            Set(ByVal value As String)
                _dbPassword = value
            End Set
        End Property

     

    I of course do some testing to ensure the database file does exist with file.io functions because if it doesnt, a blank db will be created automatically generating no errors but containing no information :-/.  Played with that issue for a bit before i figured out what was going wrong.  

     Then you are going to create a new function in this module that is going to rebuild the connection string for the entities model, so that you do not need to use the app.config call.

         Public Function dbConn() As String
            If Not (dbLocation Is Nothing) And Not (fileExists(dbLocation)) Then
                MsgBox("There is an error in your database configuration!")
                Return Nothing
            End If
            Dim connectionString As String = New System.Data.EntityClient.EntityConnectionStringBuilder() _
                With { _
                    .Metadata = "res://*/<entName>.csdl|res://*/<same>.ssdl|res://*/<same>.msl", _
                    .Provider = "System.Data.SQLite", _
                    .ProviderConnectionString = New System.Data.SqlClient.SqlConnectionStringBuilder() With _
                        { _
                            .DataSource = dbLocation, _
                            .Password = dbPassword _
                        }.ConnectionString _
                }.ConnectionString
            Return connectionString
        End Function

     

    In that function you can see I check to make sure that the dbLocation is actually set and also that windows finds it before i create the connection string.  If you see errors like table not found, then you probobly are creating a blank db project.

    Now in your edmx designer file, at the top you will see where it is pulling out the info needed for the connection.

    Partial Public Class yourEntitie

        Inherits Global.System.Data.Objects.ObjectContext

        Public Sub New()
            MyBase.New("name=yourEnt", "yourEnt")
            Me.OnContextCreated
        End Sub

     

    And you are going to make a simple change and instead of "name=yourEnt" you are going to refer to your connection builder..

     

        Public Sub New()
            MyBase.New(dbConn, "yourEnt")
            Me.OnContextCreated
        End Sub

     

    And that does it I think, although it is late, and I am new to this programming thing so...

     

    But if you have any questions please let me know, I will help as much as I can.

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems