Use DataContext to create schema in an existing blank database file?
With a DataContext and a blank file-based database that already exists, is it possible to write the Data Context classes (i.e. the ones I added to the designer) to create the db schema?
With a data context I only see the abilities to either:
- create a new database file and have its structure created at the same time,
- or to populate data into an existing database having preexisting schema.
I'm looking for a hybrid solution between those two worlds.
Update 1 (more info): My proof that a preexisting db is not compatible with the Data Context CreateDatabase() operation is this error
Database 'C:...\App_Data\IntermediateData.mdf' already exists. Choose a different database name.
based on this code
using (IntermediateClassesDataContext intermediateContext = new IntermediateClassesDataContext(_getIntermediateConn())) {
intermediateContext.CreateDatabase();
}
and this web.config entry (sanitized for SO) is what's being used:
<connectionStrings>
<add name="IntermediateData" providerName="System.Data.SqlClient" connectionString="Data Source=W500\SQLEXPRESS;AttachDbFilename=C:\Webapp\App_Data\IntermediateData.mdf;Integrated Security=True;User Instance=True"/>
... and the database mentioned already exists on disk with proper access permissions开发者_开发知识库.
Update 2 (clarification):
Context and Reasoning = File permissions and security
I don't want to give permission to the user to write new files inside the website data directory (which CreateDatabase tries to do); however I don't mind if the user can write schema and data into a preexisting data file. If a viable solution doesn't exist I might cave in and open up permissions.
What I did to get here, was create a bunch of new classes on a blank DataContext canvas as seen below. Those represent the schema I want to persist to an existing blank database:
alt text http://img8.imageshack.us/img8/5903/sampledm.png
I'd warn to stay away from CreateDatabase. Sounded great but if I recall it can't/doesn't create any associations/foreign keys.
精彩评论