Best place to set DataContext connection string?

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Best place to set DataContext connection string?

Post by OutOfTouch6947 » Wed 20 Jan 2016 17:22

I am using Entity Developer Linq to SQL and I wanted to know where is the best place tell the DataContext what connection string to use? I am guessing in the partial class I can implement OnCreated and set the connection string there? I created the model with the entity developer designer then added the .lqml file to visual studio project, I wonder if I can recreated it so it is class per file, I don't see an empty partial class for the datacontext but I do see the partial created by the designer. Is there some setting in the designer setting I need to change to create the empty partial, the one the designer will not override?

OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Re: Best place to set DataContext connection string?

Post by OutOfTouch6947 » Wed 20 Jan 2016 18:57

Why does updating the model from the database create invalid code and using statements?

using System.Data.Linq;
using System.Data.Linq.Mapping;

[System.Data.Linq.Mapping.DatabaseAttribute

Shouldn't that be Devart.Data.Linq and Devart.Data.Linq.Mapping?

OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Re: Best place to set DataContext connection string?

Post by OutOfTouch6947 » Wed 20 Jan 2016 20:03

OutOfTouch6947 wrote:Why does updating the model from the database create invalid code and using statements?

using System.Data.Linq;
using System.Data.Linq.Mapping;

[System.Data.Linq.Mapping.DatabaseAttribute

Shouldn't that be Devart.Data.Linq and Devart.Data.Linq.Mapping?
I am using Visual Studio 2015 and v5.8.762 of Entity Developer.
Ok I see I am confused, I am using the Linq To Sql Template not the LinqConnect template, the code being generated does not match what should be Linq To Sql how do I fix it?

OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Re: Best place to set DataContext connection string?

Post by OutOfTouch6947 » Wed 20 Jan 2016 20:12

What versions of the Devart dll's should I have referenced? Where are they devart dlls? I didn't see them in GAC.

OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Re: Best place to set DataContext connection string?

Post by OutOfTouch6947 » Wed 20 Jan 2016 20:47

I am convinced your Linq to SQL template doesn't generate the correct code.
This is what I end up with in my using statements.
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;

OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Re: Best place to set DataContext connection string?

Post by OutOfTouch6947 » Wed 20 Jan 2016 21:31

OutOfTouch6947 wrote:I am convinced your Linq to SQL template doesn't generate the correct code.
This is what I end up with in my using statements.
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
The only thing that worked was to delete the model and recreate it from the database.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Best place to set DataContext connection string?

Post by MariiaI » Thu 21 Jan 2016 08:03

I am using Entity Developer Linq to SQL and I wanted to know where is the best place tell the DataContext what connection string to use?
You can set connection via Entity Developer:
- check that "Use the following connection string from App.Config" is selected in the Model Properties;
- open Database Explorer and right-click on Database Connection-> Edit Connection Properties;
- change connection to the necessary one and save the changes.
Also you can set connection in the code:

Code: Select all

TestDataContext context = new TestDataContext(connectionString);
I wonder if I can recreated it so it is class per file, I don't see an empty partial class for the datacontext but I do see the partial created by the designer. Is there some setting in the designer setting I need to change to create the empty partial, the one the designer will not override?
Set the "Generate Partial Class" option to True in the Properties of the used template and the files of the partial classes will be generated (these files are generated only once when the model is created and they are not regenerated after any changes to the model to save your manual changes).
Shouldn't that be Devart.Data.Linq and Devart.Data.Linq.Mapping?
No, it shouldn't. LinqConnect is our own ORM solution, closely compatible to Microsoft LINQ to SQL: http://www.devart.com/linqconnect/ .
LinqConnect provides support for SQL Server, Oracle, MySQL, PostgreSQL and SQLite, and includes visual model designer - Entity Developer. With Entity Developer which is bundled with LinqConnect, you will be able to work with the LinqConnect models against SQL Server (Standard edition) and Oracle, MySQL, PostgreSQL and SQLite (Professional edition).

There is no LinqConnect support in Entity Developer Professional (separate product). Entity Developer Professional Edition allows designing models for NHibernate, Entity Framework, and LINQ to SQL (not LinqConnect):
http://www.devart.com/entitydeveloper/editions.html
What versions of the Devart dll's should I have referenced? Where are they devart dlls? I didn't see them in GAC.
You shouldn't add any Devart assemblies when working with Entity Developer and LINQ to SQL. Devart assemblies are required for LinqConnect support.
I am convinced your Linq to SQL template doesn't generate the correct code.
Please describe the exact steps you are performing, send us the exact code you are getting after these steps and the code you are expecting to get.

OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Re: Best place to set DataContext connection string?

Post by OutOfTouch6947 » Thu 21 Jan 2016 15:07

I do have the connection string using app.config.
I will have different connection strings for development and production as well as the need to encrypt and decrypt my connection string with code, so I am wondering where is the best place in a Partial Class for DataContext to set this? OnCreated?


It created the partials, my mistake.
Last edited by OutOfTouch6947 on Thu 21 Jan 2016 15:53, edited 1 time in total.

OutOfTouch6947
Posts: 79
Joined: Tue 02 Jun 2015 18:22

Re: Best place to set DataContext connection string?

Post by OutOfTouch6947 » Thu 21 Jan 2016 15:36

As far as the template goes all I did was create it with Entity Developer Designer then added the .lqml file to my project, I must have done something wrong after I recreated it, it was using the correct template and generating the correct code.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Best place to set DataContext connection string?

Post by MariiaI » Fri 22 Jan 2016 12:52

Yes, you can use the partial class and OnCreated() method:
https://www.devart.com/linqconnect/docs ... thods.html

You can also try passing one or another connection string to your data context constructor, depending on where the code is being executed (at the local development machine or at the production server), for example, using the compilation conditions for this:

Code: Select all

        #if DEBUG
                static string connectionString = "data source = localhost; ..."; //put here the necessary connection string
        #else
                static string connectionString = "data source = Mylocalhost; ..."; //put here the necessary connection string
        #endif
This code will determine what connection string to use according to the solution configuration (Debug or Release). And then create your DataContext object using the necessary connection string.
The choice of possible options for such scenario depends on your specific aspects, preferences, etc.

If you have any further questions, feel free to contact us.

Post Reply