C# 使用context-EF-Core更新模型的好方法
通过context-EF-core更新模型的好方法是什么 我有这样的功能:C# 使用context-EF-Core更新模型的好方法,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,通过context-EF-core更新模型的好方法是什么 我有这样的功能: public class ApplicationContext : DbContext { } public class Repository<T> { private readonly ApplicationContext context; public Repository(ApplicationContext context) { this.context = conte
public class ApplicationContext : DbContext { }
public class Repository<T>
{
private readonly ApplicationContext context;
public Repository(ApplicationContext context)
{
this.context = context;
}
public void Update(T entity)
{
context.Update(entity);
}
public async Task SaveAsync()
{
await context.SaveChangesAsync();
}
}
public void Update(T entity)
{
EntityEntry<T> entity = context.Entry(item);
entity.State = EntityState.Modified;
}
有什么好方法可以更新我的模型吗?另外,我想更改我的Update
功能,如下所示:
public class ApplicationContext : DbContext { }
public class Repository<T>
{
private readonly ApplicationContext context;
public Repository(ApplicationContext context)
{
this.context = context;
}
public void Update(T entity)
{
context.Update(entity);
}
public async Task SaveAsync()
{
await context.SaveChangesAsync();
}
}
public void Update(T entity)
{
EntityEntry<T> entity = context.Entry(item);
entity.State = EntityState.Modified;
}
公共作废更新(T实体)
{
EntityEntry实体=context.Entry(项);
entity.State=EntityState.Modified;
}
这已经很好了吗
在此之前谢谢如果您想查看代码,请转到。如果您不使用EF的存储库模式,则您的更新非常简单。