2014-09-04 1 views
0

Personel이라는 이름의 db 테이블에 필드 이름이 있습니다. ID, 성, 이름, 급여 및 위치. 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(); 
     } 
    } 
+4

있는 당신이 중 하나를 의미 할 수 있습니다? –

답변

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); 
+0

내 잘못한 마르크 감사합니다. –

관련 문제