LINQ to SQL 问题
我在使用 LINQ to SQL 实体时遇到以下问题:
// Context is DataContext that was auto genereted when i create my .dbml file
var cl = Context.Classes.ToArray();
var rm = Context.Rooms.ToArray();
List<DaySchedule> s = new List<DaySchedule>();
s.Add(new DaySchedule()
{
Class = cl[0],
DayOfWeek = 0,
Pair = 1,
Room = rm[0]
});
Context.SubmitChanges();
因此,在“SubmitChanges”之后,新的 DaySchedules 将保存到数据库中。但我没有调用 InsertOnSubmit 函数,并且我不想保存此 DaySchedule。
顺便提一句, 如果我将使用以下代码:
s.Add(new Acceron.University.DBAccess.DaySchedule()
{
Class_id = cl[0].Class_ID,
DayOfWeek = 0,
Pair = 1,
Room_id = rm[0].Room_ID
});
它不会自动保存到数据库。
您能解释一下这是错误还是功能以及我如何解决它吗?
i have following trouble with LINQ to SQL entities:
// Context is DataContext that was auto genereted when i create my .dbml file
var cl = Context.Classes.ToArray();
var rm = Context.Rooms.ToArray();
List<DaySchedule> s = new List<DaySchedule>();
s.Add(new DaySchedule()
{
Class = cl[0],
DayOfWeek = 0,
Pair = 1,
Room = rm[0]
});
Context.SubmitChanges();
so, after "SubmitChanges" new DaySchedules will be saved to db. BUT i didn't call InsertOnSubmit function and i don't want to save this DaySchedule.
BTW,
if i will using following code:
s.Add(new Acceron.University.DBAccess.DaySchedule()
{
Class_id = cl[0].Class_ID,
DayOfWeek = 0,
Pair = 1,
Room_id = rm[0].Room_ID
});
It will not be auto saved to db.
Could you explain is it bug or feature and how i can solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是设计使然。 Class 和 Room 是上下文感知实体,因为它们是根据上下文进行查询的。每当上下文感知实体添加子项时,它都会自动将这些更改排队到上下文中并将其标记为已插入。因此,如果没有自动排队功能,您就无法添加新实体。我强烈建议稍后不要调用保存更改。
It is by design. Class and Room are context-aware entities, since they were queried against the context. Anytime a context-aware entity adds children, it queues up those changes automatically to the context and marks it as inserted. So you cannot add new entities without the auto-queuing feature. I'd highly recommend not calling save changes later on.