Linq InsertOnSubmit 抛出异常:对象引用未设置到对象的实例

发布于 2024-12-08 11:54:25 字数 700 浏览 0 评论 0 原文

下面抛出异常。

传递给 InsertOnSubmit 的 course 对象属于 Linq 生成的 Course 类型。

 public ActionResult Course(CourseViewModel c)
 {
    Course course = (Course) c; //CourseViewModel is derrived from Course
    SchedulerDataContext db = new SchedulerDataContext();
    db.Courses.InsertOnSubmit(course);  // <- this is where exception is thrown
    db.SubmitChanges();
 }

此处这里,但是,我不明白他们的答案。据说我没有及时创建对象。哪个对象以及到底需要发生什么?

The following throws exception.

The course object that is being passed to InsertOnSubmit is of type Course that is generated by Linq.

 public ActionResult Course(CourseViewModel c)
 {
    Course course = (Course) c; //CourseViewModel is derrived from Course
    SchedulerDataContext db = new SchedulerDataContext();
    db.Courses.InsertOnSubmit(course);  // <- this is where exception is thrown
    db.SubmitChanges();
 }

There are already questions about this here and here, however, I don't understand their answer. Supposedly I'm not creating an object in time. Which object and what exactly needs to happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

霓裳挽歌倾城醉 2024-12-15 11:54:25

您需要先创建 Course 对象,然后再尝试插入它。

Course course = new Course { ... set the properties .. };
SchedulerDataContext db = new SchedulerDataContext();
db.Courses.InsertOnSubmit(course);
db.SubmitChanges();

You need to create the Course object before you try to insert it.

Course course = new Course { ... set the properties .. };
SchedulerDataContext db = new SchedulerDataContext();
db.Courses.InsertOnSubmit(course);
db.SubmitChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文