我在名为Personel的数据库中有一个表,字段名称是:
Id,Name,LastName,Salary和Position.I创建了一个扩展DataContext
的类,然后创建具有属性的列。然后我在这个类中尝试了itarate。但是我在下面得到了这个例外。字段名称和值是正确的我不知道这是什么错误
指定的演员表无效。
这是我的班级
public class PersonelDbContext:DataContext
{
public Table<Personel> Personel;
public PersonelDbContext()
: base("server=.\\sqlexpress;database=Project;User Id=sa;Password=1")
{
}
}
[Table(Name="Personel")]
public class Personel {
private int _id;
private string _name;
private string _lastname;
private int _salary;
private string _position;
[Column(Name="Id",Storage="_id",DbType="Int not null",IsPrimaryKey=true,IsDbGenerated=true)]
public int ID
{
get { return _id; }
set { _id = value; }
}
[Column (Name="Name",Storage="_name",DbType="Int not null identity")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[Column(Name="LastName",Storage="_lastname",DbType="Nvarchar not null")]
public string Lastname
{
get { return _lastname; }
set { _lastname = value; }
}
[Column(Name="Salary",Storage="_salary",DbType="Int not null")]
public int Salary
{
get { return _salary; }
set { _salary = value; }
}
[Column(Name="Position",Storage="_position",DbType="Nvarchar not null")]
public string Position
{
get { return _position; }
set { _position = value; }
}
}
//}
static void Main(string[] args)
{
PersonelDbContext dbcontext = new PersonelDbContext();
var query = from c in dbcontext.Personel select c;
foreach (var item in query)
{
Console.WriteLine("Id:", item.Name);
Console.WriteLine("Id:", item.Lastname);
}
Console.ReadKey();
}
}
答案 0 :(得分:3)
[Column (Name="Name",Storage="_name",DbType="Int not null identity")]
public string Name
{
get { return _name; }
set { _name = value; }
}
看起来很难。什么是dbtype?现在应该是什么?我在猜nvarchar(200) not null
或类似的东西。
另外:您的Console.WriteLine
行无法正常工作;你可能意味着其中一个:
Console.WriteLine("Id:" + item.Name);
或:
Console.WriteLine("Id:{0}", item.Name);