【问题标题】:The type arguments for method cannot be inferred from the usage in Asp.net [duplicate]无法从 Asp.net 中的用法推断方法的类型参数 [重复]
【发布时间】:2021-09-30 22:06:37
【问题描述】:

我有一个问题,我无法在 asp.net 中删除或更新实体 我想使用“AutoMapper”框架自动映射对象,但是当我尝试这样做时,它会导致错误,我不知道为什么

        /// Method for deleting entity in the Generic Repository
        public void Delete<T>(int id) where T : class
        {
            try
            {
                var entity = _context.Set<T>().Find(id);
                _context.Set<T>().Remove(entity);
                _context.SaveChanges();
            }
            catch
            {
            /// Expection error
            }
        /// Interface
        void Delete<T>(int id) where T : class;
        /// I have use the AutoMapper framework for mapping objects
        /// Also Mapper and Repository (interface) are injected in the constructor
        public void Delete(IUser entity)
        {
            Repository.Delete(_mapper.Map<UserEntity>(entity));
        }
        /// Function for delete

【问题讨论】:

  • IUserRepository 中的接口也是 ``` void Delete(IUser entity); ```

标签: c# asp.net entity-framework automapper repository-pattern


【解决方案1】:

您需要传递要删除的实体的类型。 它需要是这样的:

Repository.Delete<UserEntity>(entity.Id);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多