【问题标题】:Linq to SQL Error MappedLinq 到 SQL 错误映射
【发布时间】:2014-02-01 16:04:49
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;   
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Threading.Tasks;

namespace dbtst
{
        class person { public int id; public string name;}

        class Program
        {
            static void Main(string[] args)
            {
                DataContext db = new DataContext(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\UDMS\Documents\uhby.mdf;Integrated Security=True;");
                Table<person> tble = db.GetTable<person>();

                person a = new person();
                a.id = 5;
                tble.InsertOnSubmit(a);
                person b = new person();
                a.id = 6;
                tble.InsertOnSubmit(a);
                IEnumerable<person> query = from p in tble
                                            where p.id == 5
                                            select p;

                foreach (person e in query) { Console.WriteLine("{0}{1}", e.id, e.name); }
        }
    }
}

我一直在尝试测试 Linq-to-SQL。

错误是:

System.Data.Linq.dll 中出现“System.InvalidOperationException”类型的第一次机会异常
System.Data.Linq.dll 中出现“System.InvalidOperationException”类型的未处理异常
附加信息:类型“dbtst.person”未映射为表。

【问题讨论】:

标签: c# sql database linq


【解决方案1】:

第一次机会错误意味着代码中存在一些问题。

尝试替换下面的代码

 Table<person> tble = db.GetTable<person>();

 person a = new person();
 a.id = 5;
 tble.InsertOnSubmit(a);
 person b = new person();
 a.id = 6;
 tble.InsertOnSubmit(a);

使用以下代码:

 person a = new person();
 a.id = 5;
 person b = new person();
 b.id = 6;
 db.persons.InsertOnSubmit(a);
 db.persons.InsertOnSubmit(b);
 db.SubmitChanges();

【讨论】:

  • 想知道您是如何获得对 db.persons 的引用的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2023-03-14
  • 1970-01-01
  • 2010-11-26
  • 2010-12-17
  • 1970-01-01
相关资源
最近更新 更多