开发者

How can update an entity using Linq-to-SQL?

I have the following class that can create/delete and list entities:

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)
        {
            开发者_StackOverflow中文版db.Pais.InsertOnSubmit(pai);
        }

        public void Save()
        {
            db.SubmitChanges();
        }
    }
}

How can I update a model?


From a separate class:

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();        
}


Try the following code

Pai p = PaisRepository.Get(1);
p.Someproperty = x;
PaisRepository.Save();


Try db.Pais.ApplyChanges(pai);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜