我有以下可以创建/删除和列出实体的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace Backend.Models
{
public class PaisRepository
{
private EnviosDataContext db = new EnviosDataContext();
public IQueryable<Pai> FindAll()
{
return db.Pais;
}
public Pai Get(int id)
{
return db.Pais.FirstOrDefault(x => x.ID == id);
}
public void Add(Pai pai)
{
db.Pais.InsertOnSubmit(pai);
}
public void Delete(Pai pai)
{
db.Pais.InsertOnSubmit(pai);
}
public void Save()
{
db.SubmitChanges();
}
}
}
如何更新模型?
最佳答案
从一个单独的类:
public void UpdatePai(int id, string field1, string field2, int field3)
{
PaisRepository repository = new PaisRepository();
Pai pai = repository.Get(id);
pai.Field1 = field1;
pai.Field2 = field2;
pai.Field3 = field3;
pai.Save();
}