Index: trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs
===================================================================
--- trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs	(revision 2390)
+++ trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs	(revision 2451)
@@ -36,5 +36,8 @@
     public ModelData(Model model, byte[] data)
       : base() {
-      this.modelId = model.Id;
+      if (model.Id == -1)
+        this.model.Entity = model;
+      else
+        this.modelId = model.Id;
       this.data = data;
     }
Index: trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs
===================================================================
--- trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs	(revision 2390)
+++ trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs	(revision 2451)
@@ -32,10 +32,35 @@
 namespace HeuristicLab.Modeling.Database.SQLServerCompact {
   public class DatabaseService : IModelingDatabase {
-    private readonly string connection;
-    public DatabaseService(string connection) {
-      this.connection = connection;
-      Connect();
-      if (!ctx.DatabaseExists())
-        ctx.CreateDatabase();
+    private readonly string fileName;
+
+    public DatabaseService(string fileName) {
+      this.fileName = fileName;
+      this.readOnly = false;
+
+    }
+    public DatabaseService(string fileName, bool readOnly)
+      : this(fileName) {
+      this.readOnly = readOnly;
+    }
+
+    private string ConnectionString {
+      get {
+        string connection = "Data Source =" + fileName + ";";
+        if (this.readOnly)
+          connection += "File Mode = Read Only; Temp Path =" + System.IO.Path.GetTempPath() + ";";
+        else
+          connection += "File Mode = Shared Read;";
+        return connection;
+      }
+    }
+
+    private bool readOnly;
+    public bool ReadOnly {
+      get { return this.readOnly; }
+      set {
+        if (ctx != null)
+          throw new InvalidOperationException("Could not change readonly attribute of DatabaseService because connection is opened.");
+        this.readOnly = value;
+      }
     }
 
@@ -45,4 +70,5 @@
       Connect();
       ctx.CreateDatabase();
+      Disconnect();
     }
 
@@ -52,5 +78,5 @@
         Disconnect();
 
-      ctx = new ModelingDataContext(connection);
+      ctx = new ModelingDataContext(this.ConnectionString);
       DataLoadOptions dlo = new DataLoadOptions();
       dlo.LoadWith<ModelResult>(mr => mr.Result);
@@ -61,4 +87,7 @@
       dlo.LoadWith<Model>(m => m.Algorithm);
       ctx.LoadOptions = dlo;
+
+      if (!ctx.DatabaseExists())
+        ctx.CreateDatabase();
     }
 
@@ -73,4 +102,9 @@
     public IEnumerable<IModel> GetAllModels() {
       return ctx.Models.ToList().Cast<IModel>();
+    }
+
+    public IEnumerable<int> GetAllModelIds() {
+      return from m in ctx.Models
+             select m.Id;
     }
 
@@ -118,17 +152,22 @@
     }
 
+    public IModel GetModel(int id) {
+      var model = ctx.Models.Where(m => m.Id == id);
+      if (model.Count() == 1)
+        return model.Single();
+      return null;
+    }
+
     public void PersistModel(IModel model) {
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        Model m = (Model)model;
-        //check if model has to be updated or inserted
-        if (ctx.Models.Any(x => x.Id == model.Id)) {
-          Model orginal = ctx.Models.GetOriginalEntityState(m);
-          if (orginal == null)
-            ctx.Models.Attach(m);
-          ctx.Refresh(RefreshMode.KeepCurrentValues, m);
-        } else
-          ctx.Models.InsertOnSubmit(m);
-        ctx.SubmitChanges();
-      }
+      Model m = (Model)model;
+      //check if model has to be updated or inserted
+      if (ctx.Models.Any(x => x.Id == model.Id)) {
+        Model orginal = ctx.Models.GetOriginalEntityState(m);
+        if (orginal == null)
+          ctx.Models.Attach(m);
+        ctx.Refresh(RefreshMode.KeepCurrentValues, m);
+      } else
+        ctx.Models.InsertOnSubmit(m);
+      ctx.SubmitChanges();
     }
 
@@ -157,5 +196,5 @@
     public void PersistProblem(Dataset dataset) {
       Problem problem;
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
         if (ctx.Problems.Count() != 0)
           throw new InvalidOperationException("Could not persist dataset. A dataset is already saved in the database.");
@@ -187,16 +226,14 @@
     public void PersistPredictor(IModel model, IPredictor predictor) {
       Model m = (Model)model;
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
-        ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m));
-        ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m));
-        ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m));
-
-        ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(predictor)));
-        foreach (string variableName in predictor.GetInputVariables())
-          ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
-
-        ctx.SubmitChanges();
-      }
+      ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
+      ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m));
+      ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m));
+      ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m));
+
+      ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(predictor)));
+      foreach (string variableName in predictor.GetInputVariables())
+        ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
+
+      ctx.SubmitChanges();
     }
 
@@ -214,5 +251,5 @@
     public IAlgorithm GetOrPersistAlgorithm(string algorithmName) {
       Algorithm algorithm;
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
         var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName);
         if (algorithms.Count() == 0) {
@@ -230,5 +267,5 @@
     public IResult GetOrPersistResult(string resultName) {
       Result result;
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
         var results = ctx.Results.Where(r => r.Name == resultName);
         if (results.Count() == 0) {
@@ -246,5 +283,5 @@
     public IMetaData GetOrPersistMetaData(string metaDataName) {
       MetaData metadata;
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
         var md = ctx.MetaData.Where(r => r.Name == metaDataName);
         if (md.Count() == 0) {
@@ -277,5 +314,5 @@
 
     public void PersistModelResults(IModel model, IEnumerable<IModelResult> modelResults) {
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
         ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>());
         ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>());
@@ -296,5 +333,5 @@
 
     public void PersistInputVariableResults(IModel model, IEnumerable<IInputVariableResult> inputVariableResults) {
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
         ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>());
         ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>());
@@ -310,5 +347,5 @@
 
     public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) {
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
         ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>());
         ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>());
@@ -326,27 +363,24 @@
       Algorithm algorithm = (Algorithm)GetOrPersistAlgorithm(algorithmName);
       Variable targetVariable = (Variable)GetVariable(model.TargetVariable);
-
       Model m = (Model)CreateModel(null, model.Type, algorithm, targetVariable, model.TrainingSamplesStart, model.TrainingSamplesEnd,
         model.ValidationSamplesStart, model.ValidationSamplesEnd, model.TestSamplesStart, model.TestSamplesEnd);
-      PersistModel(m);
-      PersistPredictor(m, model.Predictor);
-
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+
+      using (ModelingDataContext ctx = new ModelingDataContext(this.ConnectionString)) {
+        ctx.Models.InsertOnSubmit(m);
+        ctx.SubmitChanges();
+        ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor)));
+        foreach (string variableName in model.Predictor.GetInputVariables())
+          ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
+
         foreach (KeyValuePair<string, double> pair in model.MetaData) {
           MetaData metaData = (MetaData)GetOrPersistMetaData(pair.Key);
           ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, pair.Value));
         }
-        ctx.SubmitChanges();
-      }
-
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+
         foreach (KeyValuePair<ModelingResult, double> pair in model.Results) {
           Result result = (Result)GetOrPersistResult(pair.Key.ToString());
           ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value));
         }
-        ctx.SubmitChanges();
-      }
-
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+
         foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == m)) {
           foreach (KeyValuePair<ModelingResult, double> variableResult in model.GetVariableResults(variable.Variable.Name)) {
@@ -355,4 +389,5 @@
           }
         }
+
         ctx.SubmitChanges();
       }
