none
How to run different Development & Release databases using C#, LINQ and O/R Designer RRS feed

  • Question

  • I am developing an application in Studio 2008 where I am using C#, LINQ, and SQL Server 2000. 

     

    I have dropped the database tables into the O/R Designer, and created a DataContext.  It created a DataSourceConnectionString where I specifed my test database.  I can run my application fine this way.

    All of the database tables that I'm referencing are identical in the test and release databases, although there are additional tables in the test database.

     

    My application runs great on the test database.  When I change the config file Connection String "Initial Catalog" to point to my production database name, the following code throws an exception when the query is run at the (query.Count()) line:

     

    The Exception thrown says "Invalid Object name 'dbo.Mytable' "

     

    I've looked in the generated code, and the variable "dbo.MyTable" is there. 

    Can anybody tell me:

    a) what is going on?

    b) what do I need to do to point the release database?

     

    The config file connection string section looks like this:

    --------------------------------Start config file snippet

    <connectionStrings>

    <add name="DefaultConnection" connectionString="Data Source = |SQL/CE|" />

    <add name="MyProjectName.Properties.Settings.DataSourceConnectionString"

    connectionString="Data Source=myDataSource;Initial Catalog=MyTestDatabaseName;Integrated Security=True"

    providerName="System.Data.SqlClient" />

    </connectionStrings>

    --------------------------------Endconfig file snippet

     

     

    ---------------------------------Start code snippet

    //// query the database for the datatypes to initialize

    MyContext_DataContext db = new MyContext_DataContext();

    db.ObjectTrackingEnabled = false; // data is read only

     

    //// Query for this data type import information

    var query =

    from df in db.MyTables

    where df.FileTypeID == fileTypeObj.FileType

    select df;

     

    if (query.Count() == 1)

    {....

    --------------------------------End code snippet

     

     

    Wednesday, May 21, 2008 12:51 PM

Answers

  • Nothing special to be done. Are you sure both tables have the same owner ?

     

    What if you use MyQuery.ToString to find out the SQL created for you by LINQ and cust/paste this in Query analyzer ?

     

    --

    Patrice

    Wednesday, May 21, 2008 2:50 PM

All replies

  • Hi BradB32,

     

     

    "All of the database tables that I'm referencing are identical in the test and release databases, although there are additional tables in the test database"

     

    Are these additional tables in your datacontext ? If it is, and if they have associations with the other tables these will change the structure of objects too (I suspect MyTable has an association with one or more of those tables that are missing)

    Wednesday, May 21, 2008 1:11 PM
  •  

    ==>"Are these additional tables in your datacontext ? "

     

    Good question.  The answer is no.  It's a simple application, and there are only 3 tables in the datacontext.  The 3 tables exist in both databases.  There are no relationships between the tables.  

     

    Despite what my specific problem is, I would appreciate an explanation of how it is supposed to work.   What should I be doing when I want to run different test and release databases using the same code and database structure?  It seems to me that it should be a simple database name change in the connection string in the config file shouldn't it?  If someone could point me to any documentation that describes how it should be done, that would be great.  I've drawn a blank in all my searches.

    Wednesday, May 21, 2008 2:26 PM
  • Nothing special to be done. Are you sure both tables have the same owner ?

     

    What if you use MyQuery.ToString to find out the SQL created for you by LINQ and cust/paste this in Query analyzer ?

     

    --

    Patrice

    Wednesday, May 21, 2008 2:50 PM
  • The same table in the two different databases had different owners.  I set them to be the same owner and it works.

     

    I'm glad to see it's as simple as changing the database name.  That makes sense and is what I expected.

     

    Thanks Patrice!
    Wednesday, May 21, 2008 4:24 PM