EF6 및 MySql을 사용하여 엔티티를 업데이트하는 데 문제가 있습니다.Entity Framework 및 MySql 업데이트 엔티티
이 코드의 일부입니다
public partial class Repositorio<T> : IRepositorio<T> where T : EntidadBase
{
private readonly IDbContext _contexto;
private IDbSet<T> _entidades;
public Repositorio(IDbContext contexto)
{
this._contexto = contexto;
}
private IDbSet<T> Entidades
{
get
{
if (_entidades == null)
_entidades = _contexto.Set<T>();
return _entidades;
}
}
public void Insert(T entity)
{
try
{
if (entity == null)
throw new ArgumentNullException("entity");
this.Entidades.Add(entity);
this._contexto.SaveChanges();
}
catch (Exception dbEx)
{
throw dbEx
}
}
public void Update(T entidad)
{
try
{
if (entidad == null)
throw new ArgumentNullException("entidad");
this._contexto.SaveChanges();
}
catch (Exception dbEx)
{
throw dbEx;
}
}
}
그리고 나는 그런 뭔가를 사용 :
var _repositorio = new Repositorio<MyEntity>(myContext);
var myEntity = _repositorio.GetById(13);
myEntity.Name = "Mooo";
_repositorio.Update(myEntity);
그것은 예외를 throw하지 않습니다,하지만 데이터는 변경이 없습니다.
Insert() 메서드는 완벽하게 작동합니다.
이것에 대한 아이디어가 있으십니까?
추 신 : NuGet Package Installer의 MySql.Data.Entities를 사용하고 있으며 EF 6.0.0, MySql.Data 6.8.3.0 및 MySql.Data.Entity.EF6 6.8.3.0이 포함되어 있습니다.
또한public void Update(T entidad)
{
try
{
if (entidad == null)
throw new ArgumentNullException("entidad");
_contexto.Entry(entidad).State = System.Data.EntityState.Modified;
this._contexto.SaveChanges();
}
catch (Exception dbEx)
{
throw dbEx;
}
}
이 저장소 패턴과 함께 UnitOfWork에 패턴을 사용하여 한 번 봐 가지고, 당신이 그것을 필요합니다
예제 코드는 insert 메서드를 보여 주지만 repo 호출은 update 메서드를 호출합니다. 호출하는 업데이트 메서드의 코드는 무엇입니까? –
안녕 Neil. 저장소의 코드 블록에서 아래로 스크롤해야 볼 수 있습니다 : – dank0ne
이 저장소 계층을 제거하는 것을 고려하십시오. 그것은 단지 당신을 괴롭힐 것입니다. 하나의 경우, 데이터의 단일 인스턴스 만 업데이트 할 수 있습니다. 하나의 트랜잭션에 많은 객체를 저장하려면 어떻게해야할까요? 주제에 대한 깊은 토론은 [이 질문] (http://stackoverflow.com/q/21758807/861716)을 참조하십시오. 그리고 [이 하나] (http://stackoverflow.com/questions/5625746/generic-repository-with-ef-4-1-what-is-the-point/5626884#5626884) 당신이'DbSet '및'DbContext'를 직접 호출합니다. –