Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\EntityFramework.6.4.0\build\EntityFramework.props" Condition="Exists('..\packages\EntityFramework.6.4.0\build\EntityFramework.props')" />
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.props" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -37,13 +37,13 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="CodeCube.Core, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CodeCube.Core.2.0.200911\lib\netstandard2.0\CodeCube.Core.dll</HintPath>
<HintPath>..\packages\CodeCube.Core.2.0.201622\lib\netstandard2.0\CodeCube.Core.dll</HintPath>
</Reference>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll</HintPath>
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
Expand Down Expand Up @@ -81,8 +81,8 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\EntityFramework.6.4.0\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.0\build\EntityFramework.props'))" />
<Error Condition="!Exists('..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.0\build\EntityFramework.targets'))" />
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.props'))" />
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.targets'))" />
</Target>
<Import Project="..\packages\EntityFramework.6.4.0\build\EntityFramework.targets" Condition="Exists('..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" />
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.targets" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" />
</Project>
4 changes: 2 additions & 2 deletions CodeCube.DataAccess.EntityFramework/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CodeCube.Core" version="2.0.200911" targetFramework="net472" />
<package id="EntityFramework" version="6.4.0" targetFramework="net472" />
<package id="CodeCube.Core" version="2.0.201622" targetFramework="net472" />
<package id="EntityFramework" version="6.4.4" targetFramework="net472" />
<package id="System.ComponentModel.Annotations" version="4.7.0" targetFramework="net472" />
<package id="System.Drawing.Common" version="4.7.0" targetFramework="net472" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@
<DocumentationFile>CodeCube.DataAccess.EntityFrameworkCore.PostgreSQL.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="CodeCube.DataAccess.EntityFrameworkCore" Version="1.0.201081" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
<ItemGroup>
<PackageReference Include="CodeCube.DataAccess.EntityFrameworkCore" Version="1.0.201742" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />

<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="CodeCube.DataAccess.EntityFrameworkCore" Version="1.0.201081" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />

<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.9" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CodeCube.DataAccess.EntityFrameworkCore.PostgreSQL.Context
/// <summary>
/// Inherit from this context to enable softdelete filtering.
/// </summary>
public abstract class ApplicationDbContext : DbContext
public abstract class ApplicationDbContext : DbContext, IApplicationDbContext
{
protected ApplicationDbContext(DbContextOptions options) : base(options)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CodeCube.DataAccess.EntityFrameworkCore.Repository
{
public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : BaseEntity
{
protected ApplicationDbContext Context;

private DbSet<TEntity> Entities;

public BaseRepository(ApplicationDbContext applicationDbContext)
{
Context = applicationDbContext;
Entities = Context.Set<TEntity>();
}

public IQueryable<TEntity> Find()
{
return Entities.AsNoTracking();
}

public async Task<TEntity> Find(Guid id)
{
return await Entities.AsNoTracking().SingleOrDefaultAsync(o => o.Id == id);
}

public IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> expression)
{
return Entities.AsNoTracking().Where(expression);
}

public async Task<TEntity> Get(Guid id)
{
return await Entities.SingleOrDefaultAsync(o => o.Id == id);
}

public async Task Add(TEntity entity)
{
await Entities.AddAsync(entity);
}

public void Update(TEntity entity)
{
Entities.Update(entity);
}

public async Task Delete(Guid id)
{
TEntity entityToDelete = await Entities.SingleOrDefaultAsync(o => o.Id == id);
if (entityToDelete == null) return;

Delete(entityToDelete);
}

public void Delete(TEntity entity)
{
Entities.Remove(entity);
}

public int Save()
{
return Context.SaveChanges();
}

public async Task<int> SaveAsync()
{
return await Context.SaveChangesAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CodeCube.DataAccess.EntityFrameworkCore.Repository
{
internal interface IBaseRepository<TEntity>
{
IQueryable<TEntity> Find();
Task<TEntity> Find(Guid id);
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> expression);
Task<TEntity> Get(Guid id);
Task Add(TEntity entity);
void Update(TEntity entity);
Task Delete(Guid id);
void Delete(TEntity entity);

int Save();
Task<int> SaveAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@
<DocumentationFile>D:\Projects Prive\GitHub\CodeCube.DataAccess\CodeCube.DataAccess.EntityFrameworkCore.Sql\CodeCube.DataAccess.EntityFrameworkCore.Sql.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="CodeCube.DataAccess.EntityFrameworkCore" Version="1.0.201081" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
<ItemGroup>
<PackageReference Include="CodeCube.DataAccess.EntityFrameworkCore" Version="1.0.201742" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />

<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="CodeCube.DataAccess.EntityFrameworkCore" Version="1.0.201081" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />

<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.9" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public TContext CreateDbContext(string connectionstring)
/// Method to retrieve the connectionstring. This is used by the factory to get the connectionstring while running migrations.
/// </summary>
/// <returns></returns>
protected abstract string GetConnectionstring();
protected virtual string GetConnectionstring()
{
throw new NotImplementedException();
}

#region privates
private static IConfiguration GetAppConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.9" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public static class ErrorConstants
{
public const string MissingConnectionstring = "No connectionstring found!. Please add a connectionstring with the name 'DatabaseConnection' to your AppSettings.json or implement the method 'GetConnectionstring'";
public const string MissingConnectionstring = "No connectionstring found!. Please add a connectionstring with the name 'DatabaseConnection' to your appsettings.json or override the method 'GetConnectionstring'";
public const string ConnectionstringParameterRequired = "The parameter 'connectionString' is required!";
public const string ModelbuilderRequired = "ModelBuilder is required!";
public const string AppSettingsBaseDirectoryNotFound = "Basedirectory for 'appsettings.json' could not be determined!";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using CodeCube.DataAccess.EntityFrameworkCore.Entities.PostgreSQL;

namespace CodeCube.DataAccess.EntityFrameworkCore.Interfaces
{
public interface IApplicationDbContext
{
/// <summary>
/// Deletes the entity with the specified id.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="id"></param>
void RemoveById<TEntity>(Guid id) where TEntity : BaseEntity, new();

/// <summary>
/// <para>
/// Saves all changes made in this context to the database.
/// </para>
/// <para>
/// This method will automatically call <see cref="M:Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.DetectChanges" /> to discover any
/// changes to entity instances before saving to the underlying database. This can be disabled via
/// <see cref="P:Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.AutoDetectChangesEnabled" />.
/// </para>
/// </summary>
/// <returns>
/// The number of state entries written to the database.
/// </returns>
/// <exception cref="T:Microsoft.EntityFrameworkCore.DbUpdateException">
/// An error is encountered while saving to the database.
/// </exception>
/// <exception cref="T:Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException">
/// A concurrency violation is encountered while saving to the database.
/// A concurrency violation occurs when an unexpected number of rows are affected during save.
/// This is usually because the data in the database has been modified since it was loaded into memory.
/// </exception>
int SaveChanges();

/// <summary>
/// <para>
/// Saves all changes made in this context to the database.
/// </para>
/// <para>
/// This method will automatically call <see cref="M:Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.DetectChanges" /> to discover any
/// changes to entity instances before saving to the underlying database. This can be disabled via
/// <see cref="P:Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.AutoDetectChangesEnabled" />.
/// </para>
/// <para>
/// Multiple active operations on the same context instance are not supported. Use 'await' to ensure
/// that any asynchronous operations have completed before calling another method on this context.
/// </para>
/// </summary>
/// <param name="acceptAllChangesOnSuccess">
/// Indicates whether <see cref="M:Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.AcceptAllChanges" /> is called after the changes have
/// been sent successfully to the database.
/// </param>
/// <param name="cancellationToken"> A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete. </param>
/// <returns>
/// A task that represents the asynchronous save operation. The task result contains the
/// number of state entries written to the database.
/// </returns>
/// <exception cref="T:Microsoft.EntityFrameworkCore.DbUpdateException">
/// An error is encountered while saving to the database.
/// </exception>
/// <exception cref="T:Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException">
/// A concurrency violation is encountered while saving to the database.
/// A concurrency violation occurs when an unexpected number of rows are affected during save.
/// This is usually because the data in the database has been modified since it was loaded into memory.
/// </exception>
Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default);
}
}