LINQ to SQL 问题

发布于 2024-11-08 10:48:51 字数 801 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-11-15 10:48:51

这是设计使然。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文