简体   繁体   English

EF Core 中的当前上下文中不存在名称 Savechanges()

[英]The name Savechanges() dies not exist in the current context in EF Core

I am migrating my code from EF to EF Core.我正在将我的代码从 EF 迁移到 EF Core。 We do have an Igenericreposirory.我们确实有一个 Igenericreposirory。 And this Igenericreposirory is inheriting by Genericreposirory.而这个 Igenericreposirory 是由 Genericreposirory 继承的。

IGenericreposirory通用存储库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace DataLayer.Repositories.Base
{
public interface IGenericRepository<T, TKey> where T : class
{
       T Add(T entity);
}
}

GenericRepository:通用存储库:

public class GeneralRepository<T, TKey> : IGenericRepository<T, TKey>
    where T : class, IEntity<TKey>, new()
 {
    protected readonly DbContext DbContext;
    internal DbSet<T> DbSet;

    public GeneralRepository(DbContext dbContext)
    {
        if (dbContext == null)
            throw new ArgumentNullException("dbContext");

        this.DbContext = dbContext;

        this.DbSet = this.DbContext.Set<T>();
    }

public virtual T Add(T entity)
    {
        #region Argument Validation

        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }

        #endregion

        var obj = DbSet.Add(entity);
        SaveChanges();
        return obj;
    }

} }

Here I am getting 2 errors while working with EF core 1 at return obj as "Cannot convert from 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry' to 'T'"在这里,我在返回 obj 处使用 EF 核心 1 时遇到 2 个错误,因为“无法从 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry' 转换为 'T'”

2 at Savechange() as "the name savechanges()" does not exists in the current context. 2 在 Savechange() 因为“名称 savechanges()”在当前上下文中不存在。

As far as i know, EF core supports all methods to fetch, Insert, ADD据我所知,EF 核心支持所有获取、插入、添加的方法

1 at return obj as "Cannot convert from 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry' to 'T'" 1 在返回 obj 为“无法从 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry' 转换为 'T'”

The signature of DbSet.Add() has been changed between EF6 and EF core. DbSet.Add()的签名已在 EF6 和 EF 核心之间更改。 In EF6 it returned the entity itself, see the documentation of DbSet.Add() in EF6 :在 EF6 中,它返回了实体本身,请参阅 EF6 中的DbSet.Add() in EF6文档:

 public virtual TEntity Add (TEntity entity);

But in EF core it has been changed to return an EntityEntry object instead, see the documentation of DbSet.Add() in EF core :但在 EF 核心中,它已更改为返回EntityEntry object,请参阅DbSet.Add() in EF core的文档:

 public virtual Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> Add (TEntity entity);

So you have to return the value of the EntityEntry.Entity property to get the tracked entity (which might be the same as your entity variable).因此,您必须返回EntityEntry.Entity属性的值才能获取跟踪的实体(可能与您的entity变量相同)。 So the code might look like this:所以代码可能如下所示:

var obj = DbSet.Add(entity);
// [...]
return obj.Entity;

2 at Savechange() as "the name savechanges()" does not exists in the current context. 2 在 Savechange() 因为“名称 savechanges()”在当前上下文中不存在。

Your are trying to call a method SaveChanges();您正在尝试调用方法SaveChanges(); on your class GeneralRepository , but there is no such method defined.在您的 class GeneralRepository上,但没有定义这样的方法。 However, the DbContext object has such a method .但是, DbContext object 有这样的方法 Simply call the method from your DbContext instead:只需从您的DbContext调用该方法即可:

var obj = DbSet.Add(entity);
this.Context.SaveChanges();
return obj.Entity;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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