我使用导航属性为实体创建了以下模型。
[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();
任何想法如何做到这一点?谢谢
像这样的东西,也可以在这里查看我有关导航属性的文章: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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句