简体   繁体   中英

Cannot resolve symbol SaveChangesAsync

I have the following problem with the .NET Core and Entity Framework. I created myself the .NET Core project, I added DbContext and all the rest. My problem is that I can download the data without the slightest problem, unfortunately I can not save them, ie I have the Add method, but I do not have the SaveChanges method.

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

using CRM.Model.Entities;

namespace CRM.Model.Concrete
{
    public abstract class ApplicationContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
        {

        }

        public DbSet<Category> Categories { get; set; }
        public DbSet<Subcategory> Subcategories { get; set; }
        public DbSet<SubcategoryL2> SubcategoriesL2 { get; set; }
        public DbSet<Event> Events { get; set; }
        public DbSet<ApplicationUser> Users { get; set; }
        public DbSet<Coupon> Coupons { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<SubcategoryL2>().ToTable("Subs");
            #region "Seed Data"

            builder.Entity<IdentityRole>().HasData(
                new { Id = "1", Name = "Admin", NormalizedName = "ADMIN" },
                new { Id = "2", Name = "User", NormalizedName = "USER" }
            );

            #endregion
        }
    }
}

ICouponRepository

using System.Threading.Tasks;
using CRM.Model.Concrete;

namespace CRM.Repository.Abstract
{
    public interface ICouponRepository
    {
        Task AddCoupon(Coupon coupon);
    }
}

CouponRepository

using System.Threading.Tasks;
using CRM.Model.Concrete;
using CRM.Repository.Abstract;

namespace CRM.Repository.Concrete
{
    public class CouponRepository : ICouponRepository
    {
        private readonly ApplicationContext _applicationContext;

        public CouponRepository(ApplicationContext applicationContext)
        {
            _applicationContext = applicationContext;
        }

        public async Task AddCoupon(Coupon coupon)
        {
           await _applicationContext.Coupons.AddAsync(coupon);
           await _applicationContext.SaveChangesAsync();
        }
    }
}

And the problem is here in CouponRepository, ie 在此处输入图片说明

I have no idea how I can fix it and why it does not work :(

CS1061 The "ApplicationContext" element does not contain the definition of "SaveChangesAsync" and the available "SaveChangesAsync" extension method was not found, which takes the first argument of the "ApplicationContext" type (does not the using directive or the kit reference?).

Second error

CS0012 C # The "IdentityDbContext <>" type is defined in an unreferenced set. You must add a reference to the set "Microsoft.AspNetCore.Identity.EntityFrameworkCore, Version = 2.2.0.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"

My project is divided into several smaller ones, that is, the main CRM project. In it there are several smaller ones:

  • CRM.Core
  • CRM.Services
  • CRM.Repository
  • CRM.Resources
  • CRM.Model

The problem is that without the slightest problem I use the ApplicationContext to retrieve data from the database, unfortunately I can not save any data with it, because the error pops up like in the picture.

when i change

public abstract class ApplicationContext : IdentityDbContext

to

public abstract class ApplicationContext : DbContext

then all is Ok, but then Identity will not work for me

Solution: The solution to my problem was to install the Microsoft.AspNetCore.Identity.EntityFrameworkCore package through the NuGet Package.

First of all you need to be calling SaveChangesAsync against the context, not the DbSet , so you should be writing this:

await _applicationContext.SaveChangesAsync();

Secondly, as a bonus, you shouldn't be using AddAsync unless you really need to, you should be doing this:

_applicationContext.Coupons.Add(coupon);

The docs state:

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

From the second error message it is clear what you have to do. Install Microsoft.AspNetCore.Identity.EntityFrameworkCore nuget package to the project where CouponRepository is located as follows:

PM> Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore -Version 2.2.0

Or you can also add Microsoft.AspNetCore.App meta-package to your CouponRepository project by adding the following item.

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

The error should go away now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM