Change from SQL CE to SQL Express

This commit is contained in:
2015-01-14 14:41:04 -05:00
parent 822f6ccdea
commit f54f92330b
21 changed files with 383 additions and 413 deletions

View File

@@ -1,99 +0,0 @@
using Common.Debug;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.SqlServerCe;
using System.Linq;
using WeatherService.Values;
namespace WeatherService.Data
{
/// <summary>
/// This class manages database connectivity and logging
/// </summary>
internal static class Database
{
private static DatabaseContext _databaseContext;
private static SqlCeConnection _connection;
public static void Connect(string databasePath)
{
_connection = new SqlCeConnection(string.Format("Data Source={0}", databasePath));
_connection.Open();
}
public static SqlCeConnection Connection
{
get
{
return _connection;
}
}
public static void Initialize(string databasePath)
{
Connect(databasePath);
// Create a database context
_databaseContext = new DatabaseContext(databasePath);
// Turn on logging if requested
if (Settings.Default.LogDatabase)
_databaseContext.Log = Tracer.Writer;
// Create the database engine
SqlCeEngine engine = new SqlCeEngine(string.Format("Data Source={0}", databasePath));
// Check to see if the database exists
if (!_databaseContext.DatabaseExists())
{
// Create the database itself
engine.CreateDatabase();
// Run the creation script
// executeScript(Resources.CreateDatabase);
}
// Compact the database
engine.Shrink();
}
public static Table<DeviceData> DeviceTable
{
get { return _databaseContext.DeviceTable; }
}
public static Table<ReadingData> ReadingTable
{
get { return _databaseContext.ReadingTable; }
}
public static IEnumerable<DeviceData> DeviceList
{
get { return from device in DeviceTable select device; }
}
public static IEnumerable<ReadingData> ReadingList(int deviceId)
{
return from reading in ReadingTable where reading.DeviceId == deviceId select reading;
}
public static IEnumerable<ReadingData> ReadingList(int deviceId, WeatherValueType valueType)
{
return from reading in ReadingTable where reading.DeviceId == deviceId && reading.Type == valueType select reading;
}
public static void SaveChanges()
{
_databaseContext.SubmitChanges(ConflictMode.ContinueOnConflict);
}
public static void DiscardChanges()
{
ChangeSet changeSet = _databaseContext.GetChangeSet();
_databaseContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Inserts);
_databaseContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Deletes);
_databaseContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates);
}
}
}

View File

@@ -1,12 +0,0 @@
using System.Data.Linq;
namespace WeatherService.Data
{
public class DatabaseContext : DataContext
{
public Table<DeviceData> DeviceTable;
public Table<ReadingData> ReadingTable;
public DatabaseContext(string databaseFile) : base(databaseFile) { }
}
}

21
Data/Device.cs Normal file
View File

@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WeatherService.Data
{
[Table("Device")]
public class Device
{
public int Id { get; set; }
[Required]
[StringLength(100)]
public string Address { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; }
public int ReadInterval { get; set; }
}
}

View File

@@ -1,28 +0,0 @@
using System.Data.Linq.Mapping;
namespace WeatherService.Data
{
[Table(Name = "Device")]
public class DeviceData
{
[Column(UpdateCheck = UpdateCheck.Never, IsDbGenerated = true, IsPrimaryKey = true)]
public int Id;
[Column(UpdateCheck = UpdateCheck.Never)]
public string Address;
[Column(UpdateCheck = UpdateCheck.Never)]
public string Name;
[Column(UpdateCheck = UpdateCheck.Never)]
public int ReadInterval;
//private readonly EntitySet<Reading> _readingSet = new EntitySet<Reading>();
//[Association(Storage = "_readingSet", OtherKey = "DeviceId", ThisKey = "Id")]
//public EntitySet<Reading> Readings
//{
// get { return _readingSet; }
// set { _readingSet.Assign(value); }
//}
}
}

19
Data/Reading.cs Normal file
View File

@@ -0,0 +1,19 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace WeatherService.Data
{
[Table("Reading")]
public class Reading
{
public int Id { get; set; }
public int DeviceId { get; set; }
public int Type { get; set; }
public double Value { get; set; }
public DateTimeOffset ReadTime { get; set; }
}
}

View File

@@ -1,60 +0,0 @@
using System;
using System.Data.Linq.Mapping;
using System.Data.SqlTypes;
using WeatherService.Values;
namespace WeatherService.Data
{
[Table(Name = "Reading")]
public class ReadingData
{
public ReadingData()
{
ReadTime = SqlDateTime.MinValue.Value;
}
[Column(UpdateCheck = UpdateCheck.Never, IsDbGenerated = true, IsPrimaryKey = true)]
public int Id
{
get;
internal set;
}
[Column(UpdateCheck = UpdateCheck.Never)]
public int DeviceId
{
get;
internal set;
}
[Column(UpdateCheck = UpdateCheck.Never)]
public WeatherValueType Type
{
get;
internal set;
}
[Column(UpdateCheck = UpdateCheck.Never)]
public double Value
{
get;
internal set;
}
[Column(UpdateCheck = UpdateCheck.Never)]
public DateTime ReadTime
{
get;
internal set;
}
//private EntityRef<DeviceData> _device;
//[Association(Storage = "_device", ThisKey = "DeviceId", OtherKey = "Id")]
//public DeviceData Device
//{
// get { return _device.Entity; }
// set { _device.Entity = value; }
//}
}
}

17
Data/Setting.cs Normal file
View File

@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WeatherService.Data
{
[Table("Setting")]
public class Setting
{
[Key]
[StringLength(500)]
public string Name { get; set; }
[Required]
[StringLength(3500)]
public string Value { get; set; }
}
}

View File

@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.SqlClient;
namespace WeatherService.Data
{
public class WeatherArchiveData : DbContext
{
private const string ConnectionStringTemplateName = "WeatherArchiveData";
private const string DatabaseNameYearTemplate = "WeatherData{0}";
private static readonly Dictionary<int, bool> DatabaseExists = new Dictionary<int, bool>();
private static string BuildConnectionString(int year)
{
var databaseName = string.Format(DatabaseNameYearTemplate, year);
var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringTemplateName].ConnectionString;
var builder = new SqlConnectionStringBuilder(connectionString) { InitialCatalog = databaseName };
return builder.ConnectionString;
}
public WeatherArchiveData(int year)
: base(BuildConnectionString(year))
{
if (DatabaseExists.ContainsKey(year))
return;
DatabaseExists[year] = Database.Exists();
if (DatabaseExists[year])
return;
Database.Create();
DatabaseExists[year] = true;
}
public virtual DbSet<Reading> Readings { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
}

19
Data/WeatherData.cs Normal file
View File

@@ -0,0 +1,19 @@
using System.Data.Entity;
namespace WeatherService.Data
{
public class WeatherData : DbContext
{
public WeatherData()
: base("name=WeatherData")
{
}
public virtual DbSet<Device> Devices { get; set; }
public virtual DbSet<Setting> Settings { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
}