Index: trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs
===================================================================
--- trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs	(revision 2371)
+++ trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs	(revision 2382)
@@ -61,5 +61,5 @@
 
     private byte[] data;
-    [Column(Storage = "data", DbType = "image", CanBeNull = false)]
+    [Column(Storage = "data", DbType = "image", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
     public byte[] Data {
       get { return this.data; }
Index: trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/Problem.cs
===================================================================
--- trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/Problem.cs	(revision 2371)
+++ trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/Problem.cs	(revision 2382)
@@ -31,5 +31,5 @@
 namespace HeuristicLab.Modeling.Database.SQLServerCompact {
   [Table(Name = "Problem")]
-  public class Problem : IProblem {
+  public class Problem {
     public Problem() {
     }
Index: trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs
===================================================================
--- trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs	(revision 2371)
+++ trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs	(revision 2382)
@@ -32,5 +32,4 @@
 namespace HeuristicLab.Modeling.Database.SQLServerCompact {
   public class DatabaseService : IModelingDatabase {
-
     private readonly string connection;
     public DatabaseService(string connection) {
@@ -72,97 +71,82 @@
     }
 
-    public IModel Persist(HeuristicLab.Modeling.IAlgorithm algorithm) {
-      GetOrCreateProblem(algorithm.Dataset);
-      return Persist(algorithm.Model, algorithm.Name, algorithm.Description);
-    }
-
-    public IModel Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) {
-      Dictionary<string, Variable> variables = GetAllVariables();
-      Algorithm algo = GetOrCreateAlgorithm(algorithmName, algorithmDescription);
-      Variable target = variables[model.TargetVariable];
-      Model m;
-
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        m = new Model(target, algo,model.Type);
-        m.TrainingSamplesStart = model.TrainingSamplesStart;
-        m.TrainingSamplesEnd = model.TrainingSamplesEnd;
-        m.ValidationSamplesStart = model.ValidationSamplesStart;
-        m.ValidationSamplesEnd = model.ValidationSamplesEnd;
-        m.TestSamplesStart = model.TestSamplesStart;
-        m.TestSamplesEnd = model.TestSamplesEnd;
-
-        ctx.Models.InsertOnSubmit(m);
-
-        ctx.SubmitChanges();
-      }
-
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor)));
-        ctx.SubmitChanges();
-      }
-
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        foreach (string inputVariable in model.InputVariables) {
-          ctx.InputVariables.InsertOnSubmit(new InputVariable(m, variables[inputVariable]));
-        }
-        ctx.SubmitChanges();
-      }
-
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        foreach (KeyValuePair<ModelingResult, double> pair in model.Results) {
-          Result result = GetOrCreateResult(pair.Key.ToString());
-          ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value));
-        }
-        ctx.SubmitChanges();
-      }
-
-      // code to store meta-information for models (gkronber (8.9.09))
-      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        foreach (KeyValuePair<string, double> pair in model.MetaData) {
-          MetaData metaData = GetOrCreateMetaData(pair.Key);
-          ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, 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)) {
-            Result result = GetOrCreateResult(variableResult.Key.ToString());
-            ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, variableResult.Value));
-          }
-        }
-        ctx.SubmitChanges();
-      }
-
-      //if connected to database return inserted model
-      if (this.ctx != null)
-        return this.ctx.Models.Where(x => x.Id == m.Id).Single();
-      return null;
-    }
-
-    #region Problem
+    public IEnumerable<IModel> GetAllModels() {
+      return ctx.Models.ToList().Cast<IModel>();
+    }
+
+    public IEnumerable<IVariable> GetAllVariables() {
+      return ctx.Variables.ToList().Cast<IVariable>();
+    }
+
+    public IEnumerable<IResult> GetAllResults() {
+      return ctx.Results.ToList().Cast<IResult>();
+    }
+
+    public IEnumerable<IResult> GetAllResultsForInputVariables() {
+      return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
+    }
+
+    public IEnumerable<IMetaData> GetAllMetaData() {
+      return ctx.MetaData.ToList().Cast<IMetaData>();
+    }
+
+    public IEnumerable<IAlgorithm> GetAllAlgorithms() {
+      return ctx.Algorithms.ToList().Cast<IAlgorithm>();
+    }
+
+    public IModel CreateModel(ModelType modelType, IAlgorithm algorithm, IVariable targetVariable,
+int trainingSamplesStart, int trainingSamplesEnd, int validationSamplesStart, int validationSamplesEnd, int testSamplesStart, int testSamplesEnd) {
+      return CreateModel(null, modelType, algorithm, targetVariable, trainingSamplesStart, trainingSamplesEnd, validationSamplesStart, validationSamplesEnd, testSamplesStart, testSamplesEnd);
+    }
+
+    public IModel CreateModel(string modelName, ModelType modelType, IAlgorithm algorithm, IVariable targetVariable,
+     int trainingSamplesStart, int trainingSamplesEnd, int validationSamplesStart, int validationSamplesEnd, int testSamplesStart, int testSamplesEnd) {
+      Variable target = (Variable)targetVariable;
+      Algorithm algo = (Algorithm)algorithm;
+      Model model = new Model(target, algo, modelType);
+      model.Name = modelName;
+      model.TrainingSamplesStart = trainingSamplesStart;
+      model.TrainingSamplesEnd = trainingSamplesEnd;
+      model.ValidationSamplesStart = validationSamplesStart;
+      model.ValidationSamplesEnd = validationSamplesEnd;
+      model.TestSamplesStart = testSamplesStart;
+      model.TestSamplesEnd = testSamplesEnd;
+
+      return model;
+    }
+
+    public void PersistModel(IModel model) {
+      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+        Model m = (Model)model;
+        Model orginal = ctx.Models.GetOriginalEntityState(m);
+        if (orginal == null)
+          ctx.Models.Attach(m);
+        ctx.Refresh(RefreshMode.KeepCurrentValues, m);
+        ctx.SubmitChanges();
+      }
+    }
+
+    public void DeleteModel(IModel model) {
+      Model m = (Model)model;
+      ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
+      ctx.ModelMetaData.DeleteAllOnSubmit(ctx.ModelMetaData.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));
+      Model orginal = ctx.Models.GetOriginalEntityState(m);
+      if (orginal == null)
+        ctx.Models.Attach(m);
+      ctx.Models.DeleteOnSubmit(m);
+      ctx.SubmitChanges();
+    }
 
     public Dataset GetDataset() {
       if (ctx.Problems.Count() != 1)
         throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database.");
-
       Problem problem = ctx.Problems.Single();
       return problem.Dataset;
-
-    }
-
-    public IProblem GetOrCreateProblem(Dataset dataset) {
-      IProblem problem;
-      if (ctx.Problems.Count() == 0)
-        problem = PersistProblem(dataset);
-      else
-        problem = ctx.Problems.Single();
-      if (problem.Dataset.ToString() != dataset.ToString())
-        throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset.");
-      return problem;
-    }
-
-    public IProblem PersistProblem(Dataset dataset) {
+    }
+
+    public void PersistProblem(Dataset dataset) {
       Problem problem;
       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
@@ -176,18 +160,44 @@
         ctx.SubmitChanges();
       }
-      return problem;
-    }
-
-    #endregion
-
-    #region Algorithm
-    public Algorithm GetOrCreateAlgorithm(string name, string description) {
+    }
+
+    public IVariable GetVariable(string variableName) {
+      var variables = ctx.Variables.Where(v => v.Name == variableName);
+      if (variables.Count() != 1)
+        throw new ArgumentException("Zero or more than one variable with the name " + variableName + " are persisted in the database.");
+      return variables.Single();
+    }
+
+    public IPredictor GetModelPredictor(IModel model) {
+      var data = (from md in ctx.ModelData
+                  where md.Model == model
+                  select md);
+      if (data.Count() != 1)
+        throw new ArgumentException("No predictor persisted for given model!");
+      return (IPredictor)PersistenceManager.RestoreFromGZip(data.Single().Data);
+    }
+
+    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();
+      }
+    }
+
+    public IAlgorithm GetOrPersistAlgorithm(string algorithmName) {
       Algorithm algorithm;
       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        var algorithms = from algo in ctx.Algorithms
-                         where algo.Name == name && algo.Description == description
-                         select algo;
+        var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName);
         if (algorithms.Count() == 0) {
-          algorithm = new Algorithm(name, description);
+          algorithm = new Algorithm(algorithmName, "");
           ctx.Algorithms.InsertOnSubmit(algorithm);
           ctx.SubmitChanges();
@@ -195,31 +205,15 @@
           algorithm = algorithms.Single();
         else
-          throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + name + " are saved in database.");
+          throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + algorithmName + " are saved in database.");
       }
       return algorithm;
     }
 
-    public IEnumerable<IAlgorithm> GetAllAlgorithms() {
-      return ctx.Algorithms.ToList().Cast<IAlgorithm>();
-    }
-    #endregion
-
-    #region Variables
-    public Dictionary<string, Variable> GetAllVariables() {
-      Dictionary<string, Variable> dict = new Dictionary<string, Variable>();
-      dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; });
-      return dict;
-    }
-    #endregion
-
-    #region Result
-    public Result GetOrCreateResult(string name) {
+    public IResult GetOrPersistResult(string resultName) {
       Result result;
       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        var results = from r in ctx.Results
-                      where r.Name == name
-                      select r;
+        var results = ctx.Results.Where(r => r.Name == resultName);
         if (results.Count() == 0) {
-          result = new Result(name);
+          result = new Result(resultName);
           ctx.Results.InsertOnSubmit(result);
           ctx.SubmitChanges();
@@ -227,60 +221,15 @@
           result = results.Single();
         else
-          throw new ArgumentException("Could not get result. More than one result with the name " + name + " are saved in database.");
+          throw new ArgumentException("Could not get result. More than one result with the name " + resultName + " are saved in database.");
       }
       return result;
     }
 
-    public IEnumerable<IResult> GetAllResults() {
-      return ctx.Results.ToList().Cast<IResult>();
-    }
-
-    public IEnumerable<IResult> GetAllResultsForInputVariables() {
-      return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
-    }
-
-    #endregion
-
-    #region ModelResult
-    public IEnumerable<IModelResult> GetModelResults(IModel model) {
-      var results = from result in ctx.ModelResults
-                    where result.Model == model
-                    select result;
-      return results.ToList().Cast<IModelResult>();
-    }
-    #endregion
-
-    #region InputVariableResults
-    public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
-      var inputResults = from ir in ctx.InputVariableResults
-                         where ir.Model == model
-                         select ir;
-      return inputResults.ToList().Cast<IInputVariableResult>();
-    }
-
-    #endregion
-
-    #region ModelMetaData
-    public IEnumerable<IModelMetaData> GetModelMetaData(IModel model) {
-      var metadata = from md in ctx.ModelMetaData
-                     where md.Model == model
-                     select md;
-      return metadata.ToList().Cast<IModelMetaData>();
-    }
-    #endregion
-
-    #region MetaData
-    public IEnumerable<IMetaData> GetAllMetaData() {
-      return ctx.MetaData.ToList().Cast<IMetaData>();
-    }
-
-    public MetaData GetOrCreateMetaData(string name) {
+    public IMetaData GetOrPersistMetaData(string metaDataName) {
       MetaData metadata;
       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
-        var md = from r in ctx.MetaData
-                 where r.Name == name
-                 select r;
+        var md = ctx.MetaData.Where(r => r.Name == metaDataName);
         if (md.Count() == 0) {
-          metadata = new MetaData(name);
+          metadata = new MetaData(metaDataName);
           ctx.MetaData.InsertOnSubmit(metadata);
           ctx.SubmitChanges();
@@ -288,46 +237,107 @@
           metadata = md.Single();
         else
-          throw new ArgumentException("Could not get metadata. More than one metadata with the name " + name + " are saved in database.");
+          throw new ArgumentException("Could not get metadata. More than one metadata with the name " + metaDataName + " are saved in database.");
       }
       return metadata;
     }
 
-    #endregion
-
-    #region Model
-    public IEnumerable<IModel> GetAllModels() {
-      return ctx.Models.ToList().Cast<IModel>();
-    }
-
-    public void UpdateModel(IModel model) {
+    public IEnumerable<IModelResult> GetModelResults(IModel model) {
+      return ctx.ModelResults.Where(mr => mr.Model == model).Cast<IModelResult>();
+    }
+    public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
+      return ctx.InputVariableResults.Where(ivr => ivr.Model == model).Cast<IInputVariableResult>();
+    }
+    public IEnumerable<IModelMetaData> GetModelMetaData(IModel model) {
+      return ctx.ModelMetaData.Where(md => md.Model == model).Cast<IModelMetaData>();
+    }
+
+    public IModelResult CreateModelResult(IModel model, IResult result, double value) {
       Model m = (Model)model;
-      Model orginal = ctx.Models.GetOriginalEntityState(m);
-      if (orginal == null)
-        ctx.Models.Attach(m);
-      ctx.Refresh(RefreshMode.KeepCurrentValues, m);
-      ctx.SubmitChanges();
-    }
-
-    public byte[] GetModelData(IModel model) {
-      var data = (from md in ctx.ModelData
-                  where md.Model == model
-                  select md);
-      if (data.Count() == 0)
-        return null;
-      return data.Single().Data;
-    }
-
-    public void UpdateModelData(IModel model, byte[] modelData) {
+      Result r = (Result)result;
+      return new ModelResult(m, r, value);
+    }
+
+    public void PersistModelResults(IModel model,IEnumerable<IModelResult> modelResults) {
+      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+        ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>());
+        ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>());
+        ctx.SubmitChanges();
+      }
+    }
+
+    public IInputVariableResult CreateInputVariableResult(IInputVariable inputVariable, IResult result, double value) {
+      InputVariable i = (InputVariable)inputVariable;
+      Result r = (Result)result;
+      return new InputVariableResult(i, r, value);
+    }
+
+    public void PersistInputVariableResults(IModel model,IEnumerable<IInputVariableResult> inputVariableResults) {
+      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+        ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>());
+        ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>());
+        ctx.SubmitChanges();
+      }
+    }
+
+    public IModelMetaData CreateModelMetaData(IModel model, IMetaData metadata, double value) {
       Model m = (Model)model;
-      ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
-      ctx.ModelData.InsertOnSubmit(new ModelData(m, modelData));
-      ctx.SubmitChanges();
-    }
-
-    public IPredictor GetModelPredictor(IModel model) {
-      byte[] data = GetModelData(model);
-      return (IPredictor)PersistenceManager.RestoreFromGZip(data);
-    }
-    #endregion
+      MetaData md = (MetaData)metadata;
+      return new ModelMetaData(m, md, value);
+    }
+
+    public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) {
+      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
+        ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>());
+        ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>());
+        ctx.SubmitChanges();
+      }
+    }
+
+    public IModel Persist(HeuristicLab.Modeling.IAlgorithm algorithm) {
+      if (ctx.Problems.Count() == 0)
+        PersistProblem(algorithm.Dataset);
+      return Persist(algorithm.Model, algorithm.Name, algorithm.Description);
+    }
+
+    public IModel Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) {
+      Algorithm algorithm = (Algorithm) GetOrPersistAlgorithm(algorithmName);
+      Variable targetVariable  = (Variable)GetVariable (model.TargetVariable);
+
+      Model m = (Model)CreateModel(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)) {
+        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)) {
+            Result result = (Result)GetOrPersistResult(variableResult.Key.ToString());
+            ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, variableResult.Value));
+          }
+        }
+        ctx.SubmitChanges();
+      }
+
+      //if connected to database return inserted model
+      if (this.ctx != null)
+        return this.ctx.Models.Where(x => x.Id == m.Id).Single();
+      return null;
+    }
   }
 }
