如何使用 linq 从 sql 获取 table?
How to get table from sql with linq?
我已经填充了 SQL 数据库并尝试使用 LINQ 查询它。
我正在尝试使用 LINQ 从 SQL table 中读取数据。我收到以下错误:
The type 'FunctionalDemo.Employees' is not mapped as a table
我错过了什么?
[TestMethod]
public void Linq_Demo_SQL()
{
DataContext dataContext = new DataContext(@"C:\Test\Database1.mdf");
Table<Employees> result = dataContext.GetTable<Employees>();
/*SQL DEMO*/
/*Create table Departments
(
ID int primary key identity,
Name nvarchar(50),
Location nvarchar(50)
)
Create table Employees
(
ID int primary key identity,
FirstName nvarchar(50),
LastName nvarchar(50),
Gender nvarchar(50),
Salary int,
DepartmentId int foreign key references Departments(Id)
)
Insert into Departments values ('IT', 'New York')
Insert into Departments values ('HR', 'London')
Insert into Departments values ('Payroll', 'Sydney')
Insert into Employees values ('Mark', 'Hastings', 'Male', 60000, 1)
Insert into Employees values ('Steve', 'Pound', 'Male', 45000,3)
Insert into Employees values ('Ben', 'Hoskins', 'Male', 70000, 1)
Insert into Employees values ('Philip', 'Hastings', 'Male', 45000,2)
Insert into Employees values ('Mary', 'Lambeth', 'Female', 30000, 2)
Insert into Employees values ('Valarie', 'Vikings', 'Female', 35000,3)
Insert into Employees values ('John', 'Stanmore', 'Male', 80000,1)
*/
}
问题在于您传递给 DataContext 的构造函数的内容。它应该是到本地服务器的正确连接字符串。必须附加mdf文件。
[Table(Name = "MyTable")]
class MyTable
{
[Column] public int Id { get; set; }
[Column] public string SomethingElse { get; set; }
}
class Program
{
static void Main(string[] args)
{
DataContext db = new DataContext(@"Data Source=(localdb)\v11.0;
Integrated Security=true;
AttachDbFileName=G:\Progress\TestingTools\ReadingThruDataContext\ReadingThruDataContext\DailyDemand2.mdf");
var firstResult = db.GetTable<MyTable>().FirstOrDefault();
var allItems = db.GetTable<MyTable>().ToList();
}
}
我已经填充了 SQL 数据库并尝试使用 LINQ 查询它。
我正在尝试使用 LINQ 从 SQL table 中读取数据。我收到以下错误:
The type 'FunctionalDemo.Employees' is not mapped as a table
我错过了什么?
[TestMethod]
public void Linq_Demo_SQL()
{
DataContext dataContext = new DataContext(@"C:\Test\Database1.mdf");
Table<Employees> result = dataContext.GetTable<Employees>();
/*SQL DEMO*/
/*Create table Departments
(
ID int primary key identity,
Name nvarchar(50),
Location nvarchar(50)
)
Create table Employees
(
ID int primary key identity,
FirstName nvarchar(50),
LastName nvarchar(50),
Gender nvarchar(50),
Salary int,
DepartmentId int foreign key references Departments(Id)
)
Insert into Departments values ('IT', 'New York')
Insert into Departments values ('HR', 'London')
Insert into Departments values ('Payroll', 'Sydney')
Insert into Employees values ('Mark', 'Hastings', 'Male', 60000, 1)
Insert into Employees values ('Steve', 'Pound', 'Male', 45000,3)
Insert into Employees values ('Ben', 'Hoskins', 'Male', 70000, 1)
Insert into Employees values ('Philip', 'Hastings', 'Male', 45000,2)
Insert into Employees values ('Mary', 'Lambeth', 'Female', 30000, 2)
Insert into Employees values ('Valarie', 'Vikings', 'Female', 35000,3)
Insert into Employees values ('John', 'Stanmore', 'Male', 80000,1)
*/
}
问题在于您传递给 DataContext 的构造函数的内容。它应该是到本地服务器的正确连接字符串。必须附加mdf文件。
[Table(Name = "MyTable")]
class MyTable
{
[Column] public int Id { get; set; }
[Column] public string SomethingElse { get; set; }
}
class Program
{
static void Main(string[] args)
{
DataContext db = new DataContext(@"Data Source=(localdb)\v11.0;
Integrated Security=true;
AttachDbFileName=G:\Progress\TestingTools\ReadingThruDataContext\ReadingThruDataContext\DailyDemand2.mdf");
var firstResult = db.GetTable<MyTable>().FirstOrDefault();
var allItems = db.GetTable<MyTable>().ToList();
}
}