diff --git a/Example/Data/Context.cs b/Example/Data/Context.cs
index 685c653..3ea98bf 100644
--- a/Example/Data/Context.cs
+++ b/Example/Data/Context.cs
@@ -21,9 +21,9 @@ public class Context : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//Default: JSON-Serialize
- //optionsBuilder.UseFileContext();
+ optionsBuilder.UseFileContext();
- optionsBuilder.UseFileContext("bson");
+ //optionsBuilder.UseFileContext("bson");
//JSON-Serialize + simple Encryption
//optionsBuilder.UseFileContext("json", "encrypted");
@@ -33,10 +33,10 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//optionsBuilder.UseFileContext("xml", "private");
//CSV
- //optionsBuilder.UseFileContext("csv");
+ //optionsBuilder.UseFileContext("csv", location: @"C:\Users\mjanatzek\Documents\Projects\t");
//Excel
- //optionsBuilder.UseFileContext("excel");
+ //optionsBuilder.UseFileContext("excel", databasename: "test", location: @"C:\Users\mjanatzek\Documents\Projects\t");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
diff --git a/FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs b/FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs
index 9e50494..4e10a9b 100644
--- a/FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs
+++ b/FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs
@@ -32,10 +32,10 @@ public static class FileContextDbContextOptionsExtensions
/// The options builder so that further configuration can be chained.
public static DbContextOptionsBuilder UseFileContext(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
- string serializer = "json", string filemanager = "default", string databasename = "")
+ string serializer = "json", string filemanager = "default", string databasename = "", string location = "")
where TContext : DbContext
=> (DbContextOptionsBuilder)UseFileContext(
- (DbContextOptionsBuilder)optionsBuilder, serializer, filemanager, databasename);
+ (DbContextOptionsBuilder)optionsBuilder, serializer, filemanager, databasename, location);
///
/// Configures the context to use FileContext.
@@ -49,14 +49,14 @@ public static DbContextOptionsBuilder UseFileContext(
/// The selection the of the file-manager to encrypt the files for example.
/// The options builder so that further configuration can be chained.
public static DbContextOptionsBuilder UseFileContext(
- [NotNull] this DbContextOptionsBuilder optionsBuilder, string serializer = "json", string filemanager = "default", string databasename = "")
+ [NotNull] this DbContextOptionsBuilder optionsBuilder, string serializer = "json", string filemanager = "default", string databasename = "", string location = "")
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
FileContextOptionsExtension extension = optionsBuilder.Options.FindExtension()
?? new FileContextOptionsExtension();
- extension = extension.WithSerializerAndFileManager(serializer, filemanager, databasename);
+ extension = extension.WithSerializerAndFileManager(serializer, filemanager, databasename, location);
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
diff --git a/FileContextCore/FileContextCore.csproj b/FileContextCore/FileContextCore.csproj
index ffc706c..cef1eed 100644
--- a/FileContextCore/FileContextCore.csproj
+++ b/FileContextCore/FileContextCore.csproj
@@ -5,7 +5,7 @@
true
key.snk
true
- 2.2.6
+ 3.0.0
morrisjdev
morrisjdev
File provider for Entity Framework Core (to be used for development purposes)
@@ -19,7 +19,7 @@
https://github.com/morrisjdev/FileContextCore
en-US
false
- 2.2.6.0
+ 3.0.0.0
diff --git a/FileContextCore/FileManager/DefaultFileManager.cs b/FileContextCore/FileManager/DefaultFileManager.cs
index 52fe668..eeb0d92 100644
--- a/FileContextCore/FileManager/DefaultFileManager.cs
+++ b/FileContextCore/FileManager/DefaultFileManager.cs
@@ -12,19 +12,24 @@ class DefaultFileManager : IFileManager
IEntityType type;
private readonly string filetype;
private readonly string databasename;
+ private readonly string _location;
- public DefaultFileManager(IEntityType _type, string _filetype, string _databasename)
+ public DefaultFileManager(IEntityType _type, string _filetype, string _databasename, string location)
{
type = _type;
filetype = _filetype;
databasename = _databasename;
+ _location = location;
}
public string GetFileName()
{
string name = type.Relational().TableName.GetValidFileName();
- string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
-
+
+ string path = string.IsNullOrEmpty(_location)
+ ? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
+ : _location;
+
Directory.CreateDirectory(path);
return Path.Combine(path, name + "." + filetype);
diff --git a/FileContextCore/FileManager/EncryptedFileManager.cs b/FileContextCore/FileManager/EncryptedFileManager.cs
index 6e61a67..98f4998 100644
--- a/FileContextCore/FileManager/EncryptedFileManager.cs
+++ b/FileContextCore/FileManager/EncryptedFileManager.cs
@@ -15,27 +15,26 @@ class EncryptedFileManager : IFileManager
private readonly string filetype;
private readonly string key;
private readonly string databasename;
+ private readonly string _location;
- public EncryptedFileManager(IEntityType _type, string _filetype, string _key, string _databasename)
+ public EncryptedFileManager(IEntityType _type, string _filetype, string _key, string _databasename, string _location)
{
type = _type;
filetype = _filetype;
key = _key;
databasename = _databasename;
+ this._location = _location;
}
public string GetFileName()
{
- string name = type.Relational().TableName;
+ string name = type.Relational().TableName.GetValidFileName();
- foreach(char c in Path.GetInvalidFileNameChars())
- {
- name = name.Replace(c, '_');
- }
-
- string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
+ string path = string.IsNullOrEmpty(_location)
+ ? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
+ : _location;
- Directory.CreateDirectory(path);
+ Directory.CreateDirectory(path);
return Path.Combine(path, name + "." + filetype + ".crypted");
}
diff --git a/FileContextCore/FileManager/PrivateFileManager.cs b/FileContextCore/FileManager/PrivateFileManager.cs
index b7343c8..15ea229 100644
--- a/FileContextCore/FileManager/PrivateFileManager.cs
+++ b/FileContextCore/FileManager/PrivateFileManager.cs
@@ -14,20 +14,25 @@ class PrivateFileManager : IFileManager
IEntityType type;
private readonly string filetype;
private readonly string databasename;
+ private readonly string _location;
- public PrivateFileManager(IEntityType _type, string _filetype, string _databasename)
+ public PrivateFileManager(IEntityType _type, string _filetype, string _databasename, string _location)
{
type = _type;
filetype = _filetype;
databasename = _databasename;
+ this._location = _location;
}
public string GetFileName()
{
string name = type.Relational().TableName.GetValidFileName();
- string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
- Directory.CreateDirectory(path);
+ string path = string.IsNullOrEmpty(_location)
+ ? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
+ : _location;
+
+ Directory.CreateDirectory(path);
return Path.Combine(path, name + ".private." + filetype);
}
diff --git a/FileContextCore/Infrastructure/Internal/FileContextOptionsExtension.cs b/FileContextCore/Infrastructure/Internal/FileContextOptionsExtension.cs
index 2445164..d301b35 100644
--- a/FileContextCore/Infrastructure/Internal/FileContextOptionsExtension.cs
+++ b/FileContextCore/Infrastructure/Internal/FileContextOptionsExtension.cs
@@ -18,6 +18,7 @@ class FileContextOptionsExtension : IDbContextOptionsExtension
{
private string _serializer = "json";
private string _filemanager = "default";
+ private string _location = "";
private string _databasename = "";
private string _logFragment;
@@ -38,6 +39,7 @@ protected FileContextOptionsExtension([NotNull] FileContextOptionsExtension copy
{
_serializer = copyFrom._serializer;
_filemanager = copyFrom._filemanager;
+ _location = copyFrom._location;
}
///
@@ -56,17 +58,20 @@ protected FileContextOptionsExtension([NotNull] FileContextOptionsExtension copy
public virtual string DatabaseName => _databasename;
+ public virtual string Location => _location;
+
///
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
///
- public virtual FileContextOptionsExtension WithSerializerAndFileManager(string serializer, string filemanager, string databasename)
+ public virtual FileContextOptionsExtension WithSerializerAndFileManager(string serializer, string filemanager, string databasename, string location)
{
FileContextOptionsExtension clone = Clone();
clone._serializer = serializer;
clone._filemanager = filemanager;
clone._databasename = databasename;
+ clone._location = location;
return clone;
}
@@ -110,7 +115,11 @@ public virtual string LogFragment
{
StringBuilder builder = new StringBuilder();
- builder.Append("serializer=").Append(_serializer).Append(";filemanager=").Append(_filemanager).Append(";databasename=").Append(_databasename).Append(' ');
+ builder
+ .Append("serializer=").Append(_serializer)
+ .Append(";filemanager=").Append(_filemanager)
+ .Append(";databasename=").Append(_databasename)
+ .Append(";location=").Append(_location).Append(' ');
_logFragment = builder.ToString();
}
diff --git a/FileContextCore/Serializer/BSONSerializer.cs b/FileContextCore/Serializer/BSONSerializer.cs
index 8f89a83..772f79a 100644
--- a/FileContextCore/Serializer/BSONSerializer.cs
+++ b/FileContextCore/Serializer/BSONSerializer.cs
@@ -7,18 +7,21 @@
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
namespace FileContextCore.Serializer
{
- class BSONSerializer : ISerializer
+ class BSONSerializer : ISerializer
{
private IEntityType entityType;
+ private readonly IPrincipalKeyValueFactory _keyValueFactory;
private string[] propertyKeys;
private readonly Type[] typeList;
- public BSONSerializer(IEntityType _entityType)
+ public BSONSerializer(IEntityType _entityType, IPrincipalKeyValueFactory _keyValueFactory)
{
entityType = _entityType;
+ this._keyValueFactory = _keyValueFactory;
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
}
@@ -52,7 +55,9 @@ private void DeserializeValues(JObject array, Dictionary n
{
JObject json = (JObject)current.Value;
- TKey key = (TKey)json.Value("__Key__").Deserialize(typeof(TKey));
+ TKey key = SerializerHelper.GetKey(_keyValueFactory, entityType,
+ propertyName => json.Value(propertyName));
+
List