G
gerry
I am looking at Linq2Sql in VS2008 and have run into a couple of things :
<Short Version>
1 - Linq To Sql designer has problems with multi-level namespace. View
Code generates error "The name SimpleNS.SubNS is not a valid identifier.
Please choose a different name." code generated ignores namespace
2 - resetting DataContext.ConnectionString , the following code
generates exceptions :
DeleteDatabase(); // tries to delete db from original
ConnectionString
if ( !DatabaseExists() )
{
string path=DatabaseFilename(); // DatabaseFilename : method
to extract db file path from connection string
Directory.CreateDirectory( Path.GetDirectoryName( path ) );
CreateDatabase(); // db file already exists - I know it
doesn't & we just dropped through !DatabaseExists()
}
</Short Version>
<Long Version>
1 - the Linq to Sql designer seems to have problems bringing up the code
window when using a 'multi-level' namespace.
using a namespace of SimpleNS, everything works fine - we get :
namespace SimpleNS
{
partial class Linq2SqlDataContext
{
}
}
setting the namespace to SimpleNS.SubNS & selecting 'View Code' causes
the error "The name SimpleNS.SubNS is not a valid identifier. Please choose
a different name."
using a single level namespace, we get :
partial class Linq2SqlDataContext
{
}
if I fix the code manually everything works fine, until I do another
'View Code' where i get the same error message again and this code is
generated :
partial class Linq2SqlDataContext
{
}
namespace SimpleNS.SubNS
{
partial class Linq2SqlDataContext
{
}
}
2 - I have my LinqToSql in a library.
I have an SqlExpress database setup for library development, the
DataContext has the Applications Settings property set to true.
I tried to create an app setting that would override the library setting
but the DataContext continued to reference the library connection string.
So instead I added modified the DataContext.OnCreated() method to change
the Connection.ConnectionString to the proper connection string for the
application.
This worked fine.
My next step was to have the DataContext check for the existance of the
database file and if it wasn't found then create it. as :
if ( !DatabaseExists() )
{
string path=DatabaseFilename(); // DatabaseFilename :
method to extract db file path from connection string
Directory.CreateDirectory( Path.GetDirectoryName( path ) );
CreateDatabase();
}
this worked fine.
Then I deleted the app db.
When I reran the app the CreateDatabase() generated an exception stating
that the db file already existed, which I know it didn't because I
completely deleted the file & parent folder and DatabaseExists() had just
returned false !
I rebooted assuming this was some type of caching issue.
After reboot the problem persisted.So I changed my code to be :
try
{
DeleteDatabase();
} catch(Exception e) { }
if ( !DatabaseExists() )
{
string path=DatabaseFilename(); // DatabaseFilename :
method to extract db file path from connection string
Directory.CreateDirectory( Path.GetDirectoryName( path ) );
CreateDatabase();
}
Now the DeleteDatabase() generated an exception : db file does not exist.
This would be fine except that the filename in the exception message was for
the db file in the library and not the application !!!
So I add the following to the DataContext to get the proper db files deleted
:
public new void DeleteDatabase()
{
File.Delete( DatabaseFilename() );
File.Delete( DatabaseFilename().Replace(".mdf","_log.LDF") );
}
I changed the App db location and the create worked fine.
Running this again, the delete deleted the proper files BUT the create threw
the same file exists exception.
</Long Version>
Maybe this Linq To Sql / DataContext stuff is not quite ready for prime time
?
Anyone know if the source for System.Data.Linq.DataContext is available
somehwere ?
I'd like to follow through the CreateDatabase/DeleteDatabase/DatabaseExists
methods to see what is going on.
Gerry
<Short Version>
1 - Linq To Sql designer has problems with multi-level namespace. View
Code generates error "The name SimpleNS.SubNS is not a valid identifier.
Please choose a different name." code generated ignores namespace
2 - resetting DataContext.ConnectionString , the following code
generates exceptions :
DeleteDatabase(); // tries to delete db from original
ConnectionString
if ( !DatabaseExists() )
{
string path=DatabaseFilename(); // DatabaseFilename : method
to extract db file path from connection string
Directory.CreateDirectory( Path.GetDirectoryName( path ) );
CreateDatabase(); // db file already exists - I know it
doesn't & we just dropped through !DatabaseExists()
}
</Short Version>
<Long Version>
1 - the Linq to Sql designer seems to have problems bringing up the code
window when using a 'multi-level' namespace.
using a namespace of SimpleNS, everything works fine - we get :
namespace SimpleNS
{
partial class Linq2SqlDataContext
{
}
}
setting the namespace to SimpleNS.SubNS & selecting 'View Code' causes
the error "The name SimpleNS.SubNS is not a valid identifier. Please choose
a different name."
using a single level namespace, we get :
partial class Linq2SqlDataContext
{
}
if I fix the code manually everything works fine, until I do another
'View Code' where i get the same error message again and this code is
generated :
partial class Linq2SqlDataContext
{
}
namespace SimpleNS.SubNS
{
partial class Linq2SqlDataContext
{
}
}
2 - I have my LinqToSql in a library.
I have an SqlExpress database setup for library development, the
DataContext has the Applications Settings property set to true.
I tried to create an app setting that would override the library setting
but the DataContext continued to reference the library connection string.
So instead I added modified the DataContext.OnCreated() method to change
the Connection.ConnectionString to the proper connection string for the
application.
This worked fine.
My next step was to have the DataContext check for the existance of the
database file and if it wasn't found then create it. as :
if ( !DatabaseExists() )
{
string path=DatabaseFilename(); // DatabaseFilename :
method to extract db file path from connection string
Directory.CreateDirectory( Path.GetDirectoryName( path ) );
CreateDatabase();
}
this worked fine.
Then I deleted the app db.
When I reran the app the CreateDatabase() generated an exception stating
that the db file already existed, which I know it didn't because I
completely deleted the file & parent folder and DatabaseExists() had just
returned false !
I rebooted assuming this was some type of caching issue.
After reboot the problem persisted.So I changed my code to be :
try
{
DeleteDatabase();
} catch(Exception e) { }
if ( !DatabaseExists() )
{
string path=DatabaseFilename(); // DatabaseFilename :
method to extract db file path from connection string
Directory.CreateDirectory( Path.GetDirectoryName( path ) );
CreateDatabase();
}
Now the DeleteDatabase() generated an exception : db file does not exist.
This would be fine except that the filename in the exception message was for
the db file in the library and not the application !!!
So I add the following to the DataContext to get the proper db files deleted
:
public new void DeleteDatabase()
{
File.Delete( DatabaseFilename() );
File.Delete( DatabaseFilename().Replace(".mdf","_log.LDF") );
}
I changed the App db location and the create worked fine.
Running this again, the delete deleted the proper files BUT the create threw
the same file exists exception.
</Long Version>
Maybe this Linq To Sql / DataContext stuff is not quite ready for prime time
?
Anyone know if the source for System.Data.Linq.DataContext is available
somehwere ?
I'd like to follow through the CreateDatabase/DeleteDatabase/DatabaseExists
methods to see what is going on.
Gerry