Naruto Reddy

Naruto Reddy

  • NA
  • 123
  • 36.4k

The name Savechanges() does not exist in the current context

Jun 16 2020 7:07 AM
Hello Experts,
 
I am migrating my code from EF to EF Core.
We do have an Igenericreposirory.
And this Igenericreposirory is inheriting by 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 where T : class
{
T Add(T entity);
}
}
GenericRepository:
public class GeneralRepository : IGenericRepository
where T : class, IEntity, new()
{
protected readonly DbContext DbContext;
internal DbSet DbSet;
public GeneralRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("dbContext");
this.DbContext = dbContext;
this.DbSet = this.DbContext.Set();
}
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'"
2 at Savechange() as "the name savechanges()" does not exists in the current context.
As far as i know , EF core supports all methods to fetch , Insert, ADD

Answers (2)