Reputation: 1585
I have populated the SQL database and am trying to query it using LINQ.
I am trying to read the data from a SQL table using LINQ. I am getting the following error:
The type 'FunctionalDemo.Employees' is not mapped as a table
What am I missing?
[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)
*/
}
Upvotes: 0
Views: 642
Reputation: 588
The problem is in what you are passing to the constructor of DataContext. It should be a proper connection string to your local server. The mdf file must be attached to it.
[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();
}
}
Upvotes: 1