我使用导航属性为我的实体提供以下模型。
[DataContract(IsReference = true)]
public partial class l_rate
{
public labor_rate()
{
this.l_rate_history = new HashSet<l_rate_hist>();
}
[DataMember]
public int l_rate_id { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public virtual ICollection<l_rate_history> l_rate_history { get; set; }
}
并且
[DataContract(IsReference = true)]
public partial class l_rate_history
{
[DataMember]
public int l_rate_history_id { get; set; }
[DataMember]
public decimal rate { get; set; }
public virtual l_rate l_rate { get; set; }
}
}
使用这些实体我正按照以下方式阅读记录....
public class testing
{
public string name { get; set; }
public decimal labo { get; set; }
}
public class lRateController : ApiController
{
private myEntities context = new myEntities();
// GET api/laborRate
public IEnumerable<testing> Getl_rate()
{
var records = from c in db.l_rate_history select new testing { name = c.l_rate.name, labo = c.rate};
return records;
}
这适用于阅读记录。接下来,我尝试使用相同的模型更新或插入新记录,以便我可以调用
context.SaveChanges();
任何想法如何做到这一点?谢谢
答案 0 :(得分:0)
像这样的东西,还可以在这里查看我的导航属性文章:http://blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_code_first
public void Setl_rate_name(int id, string name)
{
var rate = context.l_rate.Single(r=>r.Id == id);
rate.name = name;
context.SaveChanges();
}