locked
An unhandled exception occurred while processing the request. RRS feed

  • Question

  • User-605499000 posted

    I got the following message below and have no idea what to do?

    TypeLoadException: Method 'Create' in type 'Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerSqlTranslatingExpressionVisitorFactory' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=3.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.

    Below is my code for ApplicationDbContext:

    using Bumples15.Models;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    namespace Bumples15.Data
    {
        public class ApplicationDbContext : IdentityDbContext
        {
           
            public ApplicationDbContext(DbContextOptions options) : base(options) * it underlined base(options)
            {
            }
            public DbSet<Product> Product { get; set; }
            public DbSet<Order> Order { get; set; }

        }
    }

    Thanks Jen

    Monday, May 4, 2020 9:27 PM

Answers

  • User711641945 posted

    Hi bumples18,

    So which line cause the error?

    Besides,for your dependence inject IdentityRole,it should be like below:

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    
    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddDefaultUI()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 1:21 AM

All replies

  • User711641945 posted

    Hi bumples18,

    TypeLoadException: Method 'Create' in type 'Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerSqlTranslatingExpressionVisitorFactory' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=3.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.

    Which line makes such error?Could you share more code?

    For your ApplicationDbContext,it should be like below:

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    }

    Best Regards,

    Rena

    Tuesday, May 5, 2020 2:40 AM
  • User-605499000 posted

    Thank you for your reply. I put in what you gave me and still the same error.  I spent the day trying to solve the problem.  Below is a file and I wondered if that is the problem or could it be in the startup file?   Thanks for getting back to me. Jen

    SQlProductRespository file

    using Bumples15.Models;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    namespace Bumples15.Service
    {
    public class SqlBooksRepository : IRepository<Product>
    {
    private readonly ApplicationDbContext _context;
    private readonly ILogger _logger;

    public SqlBooksRepository(ApplicationDbContext context, ILogger<SqlBooksRepository> logger)
    {
    _context = context;
    _logger = logger;
    }

    public bool Add(Product item)
    {
    try
    {
    _context.Products.Add(item);
    _context.SaveChanges();
    return true;
    }
    catch (Exception ex)
    {
    _logger.LogError("Failed to add Book item to the database: " + ex.Message);
    return false;
    }
    }

    public bool Delete(Product Item)
    {
    try
    {
    Product product = Get(Item.ProductId);
    if (product != null)
    {
    _context.Products.Remove(Item);
    _context.SaveChanges();
    return true;
    }
    return false;
    }
    catch (Exception ex)
    {
    _logger.LogError("Unable to delete book: " + ex.Message);
    return false;
    }
    }

    public bool Edit(Product item)
    {
    try
    {
    _context.Products.Update(item);
    _context.SaveChanges();
    return true;
    }
    catch (Exception ex)
    {
    _logger.LogError("Unable to save book: " + ex.Message);
    }
    return false;
    }

    public Product Get(int id)
    {
    if (_context.Products.Count(x => x.ProductId == id) > 0)
    {
    return _context.Products.FirstOrDefault(x => x.ProductId == id);
    }
    return null;
    }

    public IEnumerable<Product> GetAll()
    {
    return _context.Products;
    }
    }

    public interface IRepositoryProduct
    {
    }
    }

    Wednesday, May 6, 2020 12:43 AM
  • User-605499000 posted

    Hi, I changed the DbContext to ApplicationDbContext on the file and that helped.  But I also had to go into the startup and change the setup for the Sqlserver. Below is what I changed

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {



    services.AddDbContext<ApplicationDbContext>(dbContextOptionBuilder => @changed
    dbContextOptionBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); @changed

    // .AddEntityFrameworkStores<ApplicationDbContext>();
    services.AddScoped<RoleManager<IdentityRole>>();
    services.AddScoped<IRepository<Product>, SqlBooksRepository>();
    services.AddScoped<IRepository<Order>, SqlOrdersRepository>();

    }

    .Thank you for your help,

    Jen

    Wednesday, May 6, 2020 4:08 PM
  • User711641945 posted

    Hi bumples18,

    So which line cause the error?

    Besides,for your dependence inject IdentityRole,it should be like below:

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    
    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddDefaultUI()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 1:21 AM
  • User-605499000 posted

    Hi, your answer regarding applicationdbcontext worked. Thanks,

    Jen

    Sunday, May 10, 2020 7:30 PM