小编Dan*_*san的帖子

System.MissingMethodException:找不到方法:'Void Microsoft.AspNetCore.Identity.DataProtectorTokenProvider

仍然在我发布DI 注册服务类型 .net core 3.0 的同一个项目上。现在当这个问题被修复时,我收到了新的错误。现在我的代码看起来:

    services.AddDbContext<ApplicationIdentityDbContext>(options =>
        options.UseSqlServer(configuration.GetConnectionString("Default")));

    services.AddIdentityCore<ApplicationUser>(options =>
        {
            options.Password.RequireDigit = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequiredLength = 4;

            options.SignIn.RequireConfirmedEmail = true;
            options.Tokens.ProviderMap.Add("CustomEmailConfirmation",
                new TokenProviderDescriptor(
                    typeof(CustomEmailConfirmationTokenProvider<IdentityUser>)));

            options.Tokens.EmailConfirmationTokenProvider = "CustomEmailConfirmation";

        })
        .AddEntityFrameworkStores<ApplicationIdentityDbContext>();

    services.AddTransient(o =>
    {
        var service = new CustomEmailConfirmationTokenProvider<IdentityUser>(o.GetService<IDataProtectionProvider>(), o.GetService<IOptions<DataProtectionTokenProviderOptions>>(), o.GetService<ILogger<DataProtectorTokenProvider<IdentityUser>>>());

        return service;
    });
Run Code Online (Sandbox Code Playgroud)

错误是:

System.MissingMethodException: 方法未找到:'Void Microsoft.AspNetCore.Identity.DataProtectorTokenProvider 1..ctor(Microsoft.AspNetCore.DataProtection.IDataProtectionProvider, Microsoft.Extensions.Options.IOptions1)'。

c# asp.net-core-3.0 .net-core-3.0

8
推荐指数
1
解决办法
1168
查看次数

EF Core为标识列插入显式值

我有问题将数据插入到具有外键的数据库表.我正在犯错误

当IDENTITY_INSERT设置为OFF时,无法在表'MachineTypes'中为identity列插入显式值.当IDENTITY_INSERT设置为OFF时,不能在表'SpareTypes'中插入identity列的显式值.

BaseEntity.cs

public abstract class BaseEntity
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Int64 Id { get; set; }

    public DateTime CreateDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

MachineType.cs

public class MachineType : BaseEntity
{
    [Required]
    [StringLength(50)]
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

SpareType.cs

   public class SpareType : BaseEntity
    {
        [Required]
        [StringLength(25)]
        public string Name { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

SparePart.cs

public class SparePart : BaseEntity
{
[Required]
[StringLength(100)]
public string InternalCode { get; set; }

[StringLength(4096)]
public string Description { get; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core .net-core

3
推荐指数
1
解决办法
3806
查看次数

无序列的 EF Core Average

当列表中想要按属性进行分组并取特定属性的平均值或总和时,我遇到问题,我收到错误序列不包含元素。与我放置 DefaultIfEmpty 相比,我得到不同的错误NullReferenceException: 对象引用未设置到对象的实例。

代码是这样的:

var items = _repository.GetAllItems();
var groupedItems = items.GroupBy(x=> new {Year = x.DateCreate.Year, Month = x.DateCreate.Month})
.Select(s=> new ItemForListViewModel(){
   Year = s.Key.Year,
   Month = s.Key.Month,
   AvgQnt = s.Where(x=>x.Price > 10).Average(x=>x.Qnt)
}).ToList();
Run Code Online (Sandbox Code Playgroud)

上面的代码给出错误序列不包含元素,而不是我更改

 var groupedItems = items.GroupBy(x=> new {Year = x.DateCreate.Year, Month = x.DateCreate.Month})
    .Select(s=> new ItemForListViewModel(){
       Year = s.Key.Year,
       Month = s.Key.Month,
       AvgQntPrice10 = s.Where(x=>x.Price > 10).DefaultIfEmpty().Average(x=>x.Qnt),
AvgQntPrice100 = s.Where(x=>x.Price > 100).DefaultIfEmpty().Average(x=>x.Qnt
    }).ToList();
Run Code Online (Sandbox Code Playgroud)

比我得到新的错误:NullReferenceException:对象引用未设置到对象的实例。

在数据库中,如果我运行查询,我会得到 AvgQntPrice10 的 0 和 AvgQntPrice100 的示例 15,什么是正确的。

问候,丹尼尔

c# entity-framework-core .net-core

3
推荐指数
1
解决办法
1368
查看次数

EF通用存储库多个包含

想法是拥有一个适用于所有实体的通用存储库.我设法做到了,但如果我需要有一个应该包含一个或多个其他实体的方法,那么我就有问题.我在代码中提出了一些想法,但这对我不起作用.另外我一直在考虑在EF中使用聚合函数,但我从未使用过.有人可以指导我如何管理这个吗?

  public interface IRepository<T> where T : BaseEntity
    {
        IEnumerable<T> GetAll();
        T Get(Int64 id);
        void Insert(T entity);
        void Delete(T entity);
        Task<bool> SaveChangesAsync();
        T SearchByName(Expression<Func<T, bool>> predicate);
        IEnumerable<T> GetAll(string[] includes);

    }



public class Repository<T> : IRepository<T> where T : BaseEntity
    {
        private Entities.AppContext _context;
        private DbSet<T> entities;

        public Repository(Entities.AppContext context)
        {
            _context = context;
            entities = _context.Set<T>();
        }

        public void Delete(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Remove(entity);
        }

        public T Get(long id)
        {
            return entities.SingleOrDefault(s …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core

2
推荐指数
1
解决办法
2294
查看次数

c#命名空间不能直接包含字段或方法等成员

任何人都能说出这里有什么问题吗?我只是创建一个文件夹调用模型并添加新类BaseEntity,我得到错误.直到现在我添加新课时没有收到此错误...

在此输入图像描述

c# .net-core

0
推荐指数
1
解决办法
705
查看次数