mercredi 27 avril 2016

Embedding a readonly SQLite Database in a DLL and conenct via Entity Framework

I have a requirement that my DLL should have a readonly database. I made my database as a embedded resource but i am not able to figure it out to connect to the database via entity framework.

try
        {
            var dataSet = ConfigurationManager.GetSection("system.data") as System.Data.DataSet;
            dataSet.Tables[0].Rows.Add("SQLite Data Provider"
            , ".Net Framework Data Provider for SQLite"
            , "System.Data.SQLite"
            , "System.Data.SQLite.SQLiteFactory, System.Data.SQLite");
        }
        catch (System.Data.ConstraintException) { }


        string providerName = "System.Data.SQLite";
        string serverName = "NameSpace.DB.sqlite";

        // Initialize the connection string builder for the
        // underlying provider.
        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();

        // Set the properties for the data source.
        sqlBuilder.DataSource = serverName;
        sqlBuilder.InitialCatalog = "DB.sqlite";
        sqlBuilder.IntegratedSecurity = true;

        // Build the SqlConnection connection string.
        string providerString = sqlBuilder.ToString();

        // Initialize the EntityConnectionStringBuilder.
        EntityConnectionStringBuilder entityBuilder =
        new EntityConnectionStringBuilder();

        //Set the provider name.
        entityBuilder.Provider = providerName;

        // Set the provider-specific connection string.
        entityBuilder.ProviderConnectionString = providerString;

        // Set the Metadata location.
        entityBuilder.Metadata = @"http://res*/DataModel.DB.csdl|
                                http://res*/DataModel.DB.ssdl|
                                http://res*/DataModel.DB.msl";
        Console.WriteLine(entityBuilder.ToString());

        using (EntityConnection conn =
        new EntityConnection(entityBuilder.ToString()))
        {
            conn.Open();
            Console.WriteLine("Just testing the connection.");

            _container = new MyEntities(conn);

            var usersList = _container.UserTable.ToList();
        }

It works only when we are copying the database to the client application. I don't want to provide the database as a separate file. I want to embedded it in the dll and give only the dll to other applications.

Regards, Vivek

Aucun commentaire:

Enregistrer un commentaire