You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linq to SQL报错求助:‘MYDataContext’未包含‘InsertOnSubmit’定义

解决'MYDataContext' 不含'InsertOnSubmit'定义的问题

嘿,我一眼就看出问题出在哪啦——你搞错了InsertOnSubmit方法的调用对象!这个方法不是直接在DataContext实例上调用的,而是要在对应实体的Table<T>集合上调用才行。

你看你的代码里,直接写了db.InsertOnSubmit(n),但实际上LINQ to SQL里,每个实体对应的数据库表都会在DataContext里生成一个对应的Table<T>属性(通常是表名的复数形式,比如NotificationLogs),你得先拿到这个集合,再调用它的InsertOnSubmit方法。

给你修正后的代码:

using (MYDataContext db = new MYDataContext("MYConnectionString"))
{
    NotificationLog n = new NotificationLog();
    n.result = result;
    n.context = message;
    n.tomobiles = phoneprefix + phone;
    n.datesent = DateTime.Now;
    n.SMSprovider = provider;
    // 注意这里:调用对应Table集合的InsertOnSubmit
    db.NotificationLogs.InsertOnSubmit(n);
    db.SubmitChanges();
}

另外还要提醒你两点:

  • 如果你找不到db.NotificationLogs这个属性,先去检查你的.dbml设计器文件,看看NotificationLog表有没有正确映射到实体类,属性名称是不是和表名对应(有时候会自动转复数,比如表名是NotificationLog,属性名会是NotificationLogs)
  • 你代码开头声明的MYDataContext dc = new MYDataContext("MYConnectionString");其实没用到,可以删掉,避免不必要的资源占用

内容的提问来源于stack exchange,提问作者lala lolo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:59:07