-3

我在我的项目中使用 Entity Framework 5,我想更新一条记录。我该怎么做呢?

这是我的基类。

using System;  

namespace EF_Sample09.DomainClasses  
{  
    public abstract class BaseEntity  
    {  
        public int Id { get; set; }  

        public DateTime CreatedOn { set; get; }  
        public string CreatedBy { set; get; }  

        public DateTime ModifiedOn { set; get; }  
        public string ModifiedBy { set; get; }  
    }  
}  
4

1 回答 1

0

从伟大的ADO.NET 实体框架概述中获取:

using(AdventureWorksDB aw = new 
AdventureWorksDB(Settings.Default.AdventureWorks)) {
    // find all people hired at least 5 years ago
    Query<SalesPerson> oldSalesPeople = aw.GetQuery<SalesPerson>(
        "SELECT VALUE sp " +
        "FROM AdventureWorks.AdventureWorksDB.SalesPeople AS sp " +
        "WHERE sp.HireDate < @date",
        new QueryParameter("@date", DateTime.Today.AddYears(-5)));

    foreach(SalesPerson p in oldSalesPeople) {
        // call the HR system through a webservice to see if this
        // sales person has a promotion coming (note that this
        // entity type is XML-serializable)
        if(HRWebService.ReadyForPromotion(p)) {
            p.Bonus += 10; // give a raise of 10% in the bonus
            p.Title = "Senior Sales Representative"; // give a promotion 
        }
    }

    // push changes back to the database
    aw.SaveChanges();
}

你基本上只需要:

  • 创建你的ObjectContext(或DbContext
  • 获取一些记录
  • 修改对象
  • 调用上下文的.SaveChanges()方法将这些更改写回数据库

而已!

于 2012-06-18T05:08:04.937 回答