我试图将列表集合绑定到数据网格,但它给出了一个错误。
类型"System.Data.Objects.ObjectContext"是在未被引用的程序集中定义的。必须添加对程序集"System.Data.Entity,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"的引用。
数据层代码:
public class Employees
{
public List<Employee> LoadEmployees()
{
try
{
EMployeeDB1Entities EE = new EMployeeDB1Entities();
var Employees = EE.Employees.Where(p => p.Name.StartsWith("T"));
return Employees.ToList();
// var myCollection = new ObservableCollection<Employee>(this.LoadEmployees());
}
catch
{
return null;
}
}
UI层代码
private void button1_Click(object sender, EventArgs e)
{
Employees E1 = new Employees();
// error gives in below line.
dataGridView1.DataSource = E1.LoadEmployees();
}
解决方案是什么?提前感谢。。。
错误清楚地表明您在项目中缺少System.Data.Entity类的引用,您需要通过相同的简单过程添加它。右键单击添加引用,然后单击.Net选项卡,然后从列表中选择System.Data.Entity。
你就可以走了。