我是实体框架的新手,我对savechanges的工作原理感到困惑。我的例子中可能有很多代码可以改进,但这就是我遇到的问题。
用户输入一堆选秀权。我确保用户尚未输入这些选择。 然后我将选择添加到数据库中。
var db = new myModel()
var predictionArray = ticker.Substring(1).Split(','); // Get rid of the initial comma.
var user = Membership.GetUser();
var userId = Convert.ToInt32(user.ProviderUserKey);
// Get the member with all his predictions for today.
var memberQuery = (from member in db.Members
where member.user_id == userId
select new
{
member,
predictions = from p in member.Predictions
where p.start_date == null
select p
}).First();
// Load all the company ids.
foreach (var prediction in memberQuery.predictions)
{
prediction.CompanyReference.Load();
}
var picks = from prediction in predictionArray
let data = prediction.Split(':')
let companyTicker = data[0]
where !(from i in memberQuery.predictions
select i.Company.ticker).Contains(companyTicker)
select new Prediction
{
Member = memberQuery.member,
Company = db.Companies.Where(c => c.ticker == companyTicker).First(),
is_up = data[1] == "up", // This turns up and down into true and false.
};
// Save the records to the database.
// HERE'S THE PART I DON'T UNDERSTAND.
// This saves the records, even though I don't have db.AddToPredictions(pick)
foreach (var pick in picks)
{
db.SaveChanges();
}
// This does not save records when the db.SaveChanges outside of a loop of picks.
db.SaveChanges();
foreach (var pick in picks)
{
}
// This saves records, but it will insert all the picks exactly once no matter how many picks you have.
//The fact you're skipping a pick makes no difference in what gets inserted.
var counter = 1;
foreach (var pick in picks)
{
if (counter == 2)
{
db.SaveChanges();
}
counter++;
}
我已经测试过,SaveChanges甚至不必在循环中。 以下代码也适用。
foreach (var pick in picks)
{
break;
}
db.SaveChanges()
显然,我不明白的情况正在发生。我猜我已经以某种方式将我的新选择加载为挂起的更改,但即使这是真的我也不明白我必须循环它们以保存更改。
这是根据Craig的回复更新的工作代码: 1)删除Type然后遍历结果并填充新对象。
var picks = (from prediction in predictionArray
let data = prediction.Split(':')
let companyTicker = data[0]
where !(from i in memberQuery.predictions
select i.Company.ticker).Contains(companyTicker)
select new //NO TYPE HERE
{
Member = memberQuery.member,
Company = db.Companies.Where(c => c.ticker == companyTicker).First(),
is_up = data[1] == "up", // This turns up and down into true and false.
}).ToList();
foreach (var prediction in picks)
{
if (includePrediction)
{
var p = new Prediction{
Member = prediction.Member,
Company = prediction.Company,
is_up = prediction.is_up
};
db.AddToPredictions(p);
}
}
2)或者如果我不想保存预测,我可以分离预测。
foreach (var prediction in picks) {
if (excludePrediction)
{
db.Detach(prediction)
}
}
答案 0 :(得分:3)
原因在于:
select new Prediction
{
Member = memberQuery.member,
这些行(一旦IEnumerable
被迭代; LINQ就是懒惰的):
Prediction
Prediction
与Member
附加的现有db
相关联。将实体实例与附加实体自动关联将该实体添加到关联的附加实体的上下文中。
因此,只要您开始迭代predictionArray
,上面的代码就会执行,并且您的上下文中有一个新实体。