thepirat000 / Audit.NET

An extensible framework to audit executing operations in .NET and .NET Core.
MIT License
2.25k stars 324 forks source link

Error When Converting to Entityframework core 3.1 #263

Closed MohMehrnia closed 4 years ago

MohMehrnia commented 4 years ago

When Convert Project to Error When Converting to Entityframework core 3.1 show bellow error to me

Could not load type 'Microsoft.EntityFrameworkCore.Relational Metadata Extensions' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=3.1.0.0

thepirat000 commented 4 years ago

please add more information, which libraries and versions are your referencing? could you share a minimal sample that reproduces the issue?

MohMehrnia commented 4 years ago

I Upgrade All Entity Framework Nuget Package from 2.2 to 3.1 and Use Below Code:

public sealed class EfIPMContext : AuditDbContext
    {
        public string LoginedUserName { get; set; }
        public Guid AreaID { get; private set; }
        public DbSet<DocumentRequirementDescription> DocumentsRequirementDescription { get; private set; }
        public DbSet<DocumentRequirementDescriptionSchedule> DocumentsRequirementDescriptionSchedule { get; private set; }
        public DbSet<DocumentRequirementDescriptionWBS> DocumentsRequirementDescriptionWBS { get; private set; }
        public DbSet<DocumentRequirementDescriptionWBSRealValue> DocumentsRequirementDescriptionWBSRealValue { get; private set; }
        public DbSet<DocumentRequirementDescriptionAttachment> DocumentsRequirementDescriptionAttachment { get; private set; }
        public DbSet<DocumentRequirementDescriptionEvent> DocumentRequirementDescriptionEvents { get; private set; }
        public DbSet<ContractorInformation> ContractorsInformation { get; private set; }
        public DbSet<ContractInformation> ContractsInformation { get; private set; }
        public DbSet<ContractFinanceDocumentInformation> ContractFinanceDocumentInformation { get; private set; }
        public DbSet<DocumentRequirementDescriptionDefaultStage> DocumentRequirementDescriptionDefaultStage { get; private set; }
        public DbSet<DocumentRequirementDescriptionMasterPlan> DocumentsRequirementDescriptionMasterPlan { get; private set; }
        public DbSet<DocumentRequirementDescriptionMasterPlanRealValue> DocumentsRequirementDescriptionMasterPlanRealValue { get; private set; }
        public DbSet<ContractPeople> ContractPeople { get; private set; }
        public DbSet<AppConstant> AppConstant { get; private set; }
        public DbSet<ApplicationRole> ApplicationRole { get; private set; }
        public DbSet<ApplicationRolePermission> ApplicationRolePermission { get; private set; }
        public DbSet<Tender> Tenders { get; private set; }
        public DbSet<ApplicationMember> ApplicationMembers { get; private set; }
        public DbSet<ContractAttachmentInfo> ContractAttachmentsInfo { get; private set; }
        public DbSet<Project> Projects { get; private set; }
        public DbSet<ApplicationRoleDocumentRequirementDescriptionDefaultStage> ApplicationRoleDocumentRequirementDescriptionDefaultStage { get; private set; }
        public DbSet<ProjectComment> ProjectComments { get; private set; }
        public DbSet<AccessDenideIDs> AccessDenideIDs { get; private set; }

        public DbSet<DocumentsRequirementDescriptionReport> DocumentsRequirementDescriptionReport { get; private set; }
        public DbSet<DocumentsRequirementDescriptionMasterPlanReport> DocumentsRequirementDescriptionMasterPlanReport { get; private set; }
        public DbSet<ProgressCumulativeDailyReportByEntryDate> ProgressCumulativeDailyReportByEntryDate { get; private set; }
        public DbSet<ProgressCumulativeDailyReportByTask> ProgressCumulativeDailyReportByTask { get; private set; }
        public DbSet<ContractFinanceDocumentInformationReport> ContractFinanceDocumentInformationReport { get; private set; }
        public DbSet<ActualProgressByDate> ActualProgressByDate { get; private set; }
        public DbSet<DocumentRequirementDescriptionEventsTagInfo> DocumentRequirementDescriptionEventsTagInfo { get; private set; }
        public DbSet<ContractInformationReport> ContractsInformationReport { get; private set; }
        public DbSet<TenderReport> TendersReport { get; private set; }
        public DbSet<DocumentRequirementDescriptionEventsReport> DocumentRequirementDescriptionEventsReport { get; private set; }
        public DbSet<ProjectsReport> ProjectsReport { get; private set; }
        public DbSet<SelectTenderForTag> SelectTenderForTag { get; private set; }
        public DbSet<ActualProgressByTaskByDate> ActualProgressByTaskByDate { get; private set; }
        public DbSet<DRDCriticalEvents> DRDCriticalEvents { get; private set; }
        public DbSet<ActualProgressPerDate> ActualProgressPerDate { get; private set; }
        public DbSet<CriticalDelayReports> CriticalDelayReports { get; private set; }
        public DbSet<ProgressGanttChartInfo> ProgressGanttChartInfo{ get; set; }

        public EfIPMContext(DbContextOptions<EfIPMContext> options, string username, Guid areaID) : base(options)
        {
            LoginedUserName = username;
            AreaID = areaID;
        }
        public EfIPMContext(Guid areaID) : base()
        {
            try
            {
                AreaID = areaID;
                Audit.Core.Configuration.Setup().UseNullProvider();
            }
            catch (Exception) { }
        }
        public EfIPMContext(string username, Guid areaID) : base()
        {
            LoginedUserName = username;
            try
            {
                AreaID = areaID;
                Audit.Core.Configuration.Setup().UseNullProvider();
            }
            catch (Exception) { }
        }
        public EfIPMContext()
        {
            try
            {
                AreaID = AreaConfig.AreaID;
                Audit.Core.Configuration.Setup().UseNullProvider();
            }
            catch (Exception) { }
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer(IPMDBConnection.ConnectionString, (options) => { });
            optionsBuilder.EnableSensitiveDataLogging(true);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfiguration<DocumentRequirementDescription>(new DocumentRequirementDescriptionConfig(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionSchedule>(new DocumentRequirementDescriptionScheduleConfig(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionWBS>(new DocumentRequirementDescriptionWBSConfig(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionWBSRealValue>(new DocumentRequirementDescriptionWBSRealValueConfig(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionAttachment>(new DocumentRequirementDescriptionAttachmentConfig(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionEvent>(new DocumentRequirementDescriptionEventConfig(AreaID));
            modelBuilder.ApplyConfiguration<ContractorInformation>(new ContractorInformationConfig(AreaID));
            modelBuilder.ApplyConfiguration<ContractInformation>(new ContractInformationConfig(AreaID));
            modelBuilder.ApplyConfiguration<ContractFinanceDocumentInformation>(new ContractFinanceDocumentInformationConfig(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionMasterPlan>(new DocumentRequirementDescriptionMasterPlanConfig(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionMasterPlanRealValue>(new DocumentRequirementDescriptionMasterPlanRealValueConfig(AreaID));
            modelBuilder.ApplyConfiguration<AppConstant>(new AppConstantConfig(AreaID));
            modelBuilder.ApplyConfiguration<ContractPeople>(new ContractPeopleConfig(AreaID));
            modelBuilder.ApplyConfiguration<ApplicationRole>(new ApplicationRoleConfig(AreaID));
            modelBuilder.ApplyConfiguration<ApplicationRolePermission>(new ApplicationRolePermissionConfig(AreaID));
            modelBuilder.ApplyConfiguration<ApplicationRoleDocumentRequirementDescriptionDefaultStage>(new ApplicationRoleDocumentRequirementDescriptionDefaultStageConfig(AreaID));
            modelBuilder.ApplyConfiguration<Tender>(new TenderConfig(AreaID));
            modelBuilder.ApplyConfiguration<ApplicationMember>(new ApplicationMemberConfig(AreaID));
            modelBuilder.ApplyConfiguration<ContractAttachmentInfo>(new ContractAttachmentInfoConfig(AreaID));
            modelBuilder.ApplyConfiguration<Project>(new ProjectConfig(AreaID));
            modelBuilder.ApplyConfiguration<ProjectComment>(new ProjectCommentConfig(AreaID));

            //Query
            modelBuilder.ApplyConfiguration<DocumentsRequirementDescriptionReport>(new BaseViewConfig<DocumentsRequirementDescriptionReport>(AreaID));
            modelBuilder.ApplyConfiguration<DocumentsRequirementDescriptionMasterPlanReport>(new BaseViewConfig<DocumentsRequirementDescriptionMasterPlanReport>(AreaID));
            modelBuilder.ApplyConfiguration<ProgressCumulativeDailyReportByEntryDate>(new BaseViewConfig<ProgressCumulativeDailyReportByEntryDate>(AreaID));
            modelBuilder.ApplyConfiguration<ProgressCumulativeDailyReportByTask>(new BaseViewConfig<ProgressCumulativeDailyReportByTask>(AreaID));
            modelBuilder.ApplyConfiguration<ContractFinanceDocumentInformationReport>(new BaseViewConfig<ContractFinanceDocumentInformationReport>(AreaID));
            modelBuilder.ApplyConfiguration<ActualProgressByDate>(new BaseViewConfig<ActualProgressByDate>(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionEventsTagInfo>(new BaseViewConfig<DocumentRequirementDescriptionEventsTagInfo>(AreaID));
            modelBuilder.ApplyConfiguration<ContractInformationReport>(new BaseViewConfig<ContractInformationReport>(AreaID));
            modelBuilder.ApplyConfiguration<TenderReport>(new BaseViewConfig<TenderReport>(AreaID));
            modelBuilder.ApplyConfiguration<DocumentRequirementDescriptionEventsReport>(new BaseViewConfig<DocumentRequirementDescriptionEventsReport>(AreaID));
            modelBuilder.ApplyConfiguration<ProjectsReport>(new BaseViewConfig<ProjectsReport>(AreaID));
            modelBuilder.ApplyConfiguration<SelectTenderForTag>(new BaseViewConfig<SelectTenderForTag>(AreaID));
            modelBuilder.ApplyConfiguration<ActualProgressByTaskByDate>(new BaseViewConfig<ActualProgressByTaskByDate>(AreaID));
            modelBuilder.ApplyConfiguration<DRDCriticalEvents>(new BaseViewConfig<DRDCriticalEvents>(AreaID));
            modelBuilder.ApplyConfiguration<ActualProgressPerDate>(new BaseViewConfig<ActualProgressPerDate>(AreaID));
            modelBuilder.ApplyConfiguration<CriticalDelayReports>(new BaseViewConfig<CriticalDelayReports>(AreaID));
            modelBuilder.ApplyConfiguration<ProgressGanttChartInfo>(new BaseViewConfig<ProgressGanttChartInfo>(AreaID));
        }

        //public override void OnScopeCreated(AuditScope auditScope)
        //{
        //    Database.BeginTransaction();
        //}

        //public override void OnScopeSaving(AuditScope auditScope)
        //{
        //    try
        //    {
        //        LogManagement.LogDB(new TraceChanges
        //        {
        //            AreaID = AreaID.ToString(),
        //            MemberUsername = LoginedUserName,
        //            scope = auditScope.GetEntityFrameworkEvent(),
        //            LastChangesDate = PersianDateTime.Today.ToShortDateString()
        //        });
        //    }
        //    catch (Exception)
        //    {
        //        Database.CurrentTransaction.Rollback();
        //        throw;
        //    }
        //    Database.CurrentTransaction.Commit();
        //}

When Edit data show below error: Could not load type 'Microsoft.EntityFrameworkCore.Relational Metadata Extensions' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=3.1.0.0

thepirat000 commented 4 years ago

Which Audit.* package versions are you referencing? please make sure you are using the latest version. Audit.EntityFramework.Core 14.9.1

MohMehrnia commented 4 years ago

I ensure used last audit version by upgrade all NuGet package

thepirat000 commented 4 years ago

I'm unable to reproduce the issue. I've created a new console app with following csproj code:

<PropertyGroup>
  <TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
  <PackageReference Include="audit.entityframework.core" Version="14.9.1" />
  <PackageReference Include="microsoft.entityframeworkcore" Version="3.1.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
</ItemGroup>

And tested adding a new entity to a SQL server and saving/audit succeeded.

Could you share a minimal project that reproduces the problem?

MohMehrnia commented 4 years ago

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
    <Configurations>Debug;Release;Debug Mobile App</Configurations>
  </PropertyGroup>

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

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>bin\Debug</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Audit.EntityFramework.Core" Version="14.9.1" />
    <PackageReference Include="AutoMapper" Version="9.0.0" />
    <PackageReference Include="Confluent.Kafka" Version="1.3.0" />
    <PackageReference Include="MD.PersianDateTime.Standard" Version="1.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="3.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="RavenDB.Client" Version="4.2.6" />
    <PackageReference Include="RestSharp" Version="106.6.10" />
    <PackageReference Include="Z.BulkOperations" Version="3.0.5" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\AIKI.CO.IPM.DM.BusinessObject.Shared\AIKI.CO.IPM.DM.BusinessObject.Shared.csproj" />
    <ProjectReference Include="..\AIKI.CO.IPM.Utility\AIKI.CO.IPM.Utility.csproj" />
  </ItemGroup>

</Project>

[Error Image](https://pasteboard.co/ILyMOJn.png)
thepirat000 commented 4 years ago

Still unable to reproduce. Please share a minimal runnable project that fails.

or share the code that is failing and the stack trace.

Maybe cleaning your solution, deleting bin/obj folders could help, (if you didn't try that already)

MohMehrnia commented 4 years ago

Here is stack trace

at Audit.EntityFramework.DbContextHelper.GetEntityName(DbContext dbContext, EntityEntry entry)
   at Audit.EntityFramework.DbContextHelper.CreateAuditEvent(IAuditDbContext context)
   at Audit.EntityFramework.DbContextHelper.SaveChanges(IAuditDbContext context, Func`1 baseSaveChanges)
   at Audit.EntityFramework.AuditDbContext.SaveChanges()
   at Arch.EntityFrameworkCore.UnitOfWork.UnitOfWork`1.SaveChanges(Boolean ensureAutoHistory)
   at AIKI.CO.IPM.Presenter.PresenterDocumentRequirementDescription.Update() in D:\MySourceCodes\Projects-Windows\IntelligentPlanningManagement\AIKI.CO.IPM.Presenter\PresenterDocumentRequirementDescription.cs:line 95
   at AIKI.CO.IPM.UI.DocumentRequirementDescription.ucDocumentRequirementDescription.UpdateRecord() in D:\MySourceCodes\Projects-Windows\IntelligentPlanningManagement\AIKI.CO.IPM.UI.DocumentRequirementDescription\ucDocumentRequirementDescription.cs:line 126
   at AIKI.CO.IPM.UI.Basics.UserControls.ucCRUDBasics.btnOK_Click(Object sender, EventArgs e) in D:\MySourceCodes\Projects-Windows\IntelligentPlanningManagement\AIKI.CO.IPM.UI.Basics\UserControls\ucCRUDBasics.cs:line 123

and I try cleaning your solution, deleting bin/obj folders but not working. When Change AuditDbContext to DbContext is Done and do not exists any error in runtime Here is Full Source Code: Download

thepirat000 commented 4 years ago

I think the problem is mixing .net framework projects with .net core projects,

You are referencing the net461 version of Audit.EF.Core library on your AIKI.CO.IPM.UI.Basics.csproj. That version references and uses extensions defined on EntityFramework 2.2, which were removed on EntityFramework 3.*. But since you are referencing Microsoft.EntityFrameworkCore 3.1.0 explicitly on your AIKI.CO.IPM.Presenter.csproj, that generates the problem.

Check this #IF: https://github.com/thepirat000/Audit.NET/blob/59c03659d5a63745e8aa17fa4087f4a48ee1623b/src/Audit.EntityFramework/DbContextHelper.Core.cs#L153

The library referenced on your main project : <HintPath>..\packages\Audit.EntityFramework.Core.14.9.1\lib\net461\Audit.EntityFramework.Core.dll</HintPath>

Maybe you could try by removing the Audit.EF.Core reference from your .net framework project, and reference them on your .net core projects only

image

MohMehrnia commented 4 years ago

is not work, I replace with Z Entity Framework Core Audit and does not any error but audit.net is better

thepirat000 commented 4 years ago

Please upgrade your references for Audit.* packages to 15.0.0

The latest version of Audit.EntityFramework.Core allows you to target the EF Core 3.1.0 package from a .NET framework >= 4.7.2 project.

Now the mapping is like this:

image

MohMehrnia commented 4 years ago

All packages I’d upgrade latest version and try try try but is not work

thepirat000 commented 4 years ago

could you share an updated project that fails?