Base solution for your next web application

Activities of "fengol"

Maybe a simpler test. If your application service implements the project's AppServiceBase then it inherits a function called GetCurrentUserAsync which returns the calling user.

Even simpler; ApplicationService contains an object called AbpSession with the calling user's Id.

Create an empty interface for your domain entities to inherit. Then create an extension method and overload the Update to take the entity and a user Id (which you pass from AbpSession or whatever). The method must check if the entity implements your interface, then check if the passed user is the entity's creator before updating otherwise it just updates the entity as normal. You must decide what to do if the user is not the owner; in my example below I just throw an exception.

<ins>You must just remember to use the new Update method everywhere instead of the normal update.</ins>

Example below:

The interface

public interface IMustOwnEntity
{
}

Extension Class

public static class RepositoryExtensions
{
    public static TEntity Update<TEntity>(this IRepository<TEntity> repository, TEntity entity, long userId) where TEntity : class, IEntity<int>, ICreationAudited
    {
        if (entity is IMustOwnEntity)
        {
            var exists = repository.FirstOrDefaultAsync((record) => record.Id == entity.Id && record.CreatorUserId == userId);

            if (exists != null)
            {
                return repository.Update(entity);
            }
            else
            {
                throw new Exception("Record does not belong to user");
            }
        }
        else
        {
            return repository.Update(entity);
        }
    }
}

P.S. The extension method will only be available on repositories where the entity implements ICreationAudited, IAudited, or IFullAudited so that there's a CreatorUserId to test against.

Thanks, not a problem.

Just so I understand, you want a delegate to execute after an entity inserted via the DBContext so you can get the Id that was used?

While you might be able to use the older Entity Framework tools the real reason for the problem is the .EntityFrameworkCore project file is missing a reference to the CLI tools.

Edit the .EntityFrameworkCore.csproj file and add this reference package

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />

I also recommend updating the versions of the other packages

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
  </ItemGroup>

Then you must add the .NET CLI tool reference by adding another ItemGroup

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
  </ItemGroup>

Now run dotnet restore in either the solution or the project folder and it will download the EFCore tools and enable them for the .EntityFrameworkCore project.

You should then be able to run dotnet ef and see the EFCore driver has been loaded.

Showing 1 to 5 of 5 entries