Asp.net mvc 使用实体框架的导航属性更新数据库
使用导航属性,我的实体有以下模型Asp.net mvc 使用实体框架的导航属性更新数据库,asp.net-mvc,entity-framework,asp.net-web-api,linq-to-entities,Asp.net Mvc,Entity Framework,Asp.net Web Api,Linq To Entities,使用导航属性,我的实体有以下模型 [DataContract(IsReference = true)] public partial class l_rate { public labor_rate() { this.l_rate_history = new HashSet<l_rate_hist>(); } [DataMember] public int l_
[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; }
}
使用这些实体,我按照以下方式读取记录
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;
}
有什么办法吗?谢谢诸如此类的东西,也可以在这里查看我关于导航属性的文章:
我不太确定你的问题是什么,你能发布你的更新代码吗?@LukeMcGregor基本上我不知道如何将数据保存回数据库。我可以很好地读取数据,但不知道如何将其写回数据库
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();
public void Setl_rate_name(int id, string name)
{
var rate = context.l_rate.Single(r=>r.Id == id);
rate.name = name;
context.SaveChanges();
}