【发布时间】:2012-12-15 08:52:26
【问题描述】:
我们有一个名为Model1 的模型,它有两个属性。 Model1.Time、Model1.Text和Model1.Value,当然也有ID作为要求。
我们从 SOAP 服务接收值并将它们注册到我们的数据库,但问题是:我们必须检查这些值是否已经可用。最好的方法是什么?
基本上我们使用的是 linq 查询。
DataContext db = new DataContext();
var x = (from y in db.Models where y.Value != new.Value && y.Text != new.Text select y).toList();
if { (x.Count == null) db.Models.Add(new); }
else {
foreach (var y in x) {
if (y.Time < new.Time) {
y.Time = new.Time;
y.Value = new.Value;
y.Text = new.Text;
db.ObjectStateManager.ChangeObjectState(y, EntityState.Modified);
}
}
db.SaveChanges();
}
你这么认为吗?这是唯一可能的方法还是有更合适的解决方案?
【问题讨论】:
-
您没有从请求中获取模型的 id 吗?
-
不,没有可用的 ID。所以 EF 正在创建一个增量 ID
标签: c# asp.net-mvc entity-framework linq-to-entities