2011-04-07 5 views
1

큰 C# 프로젝트를 상속했으며 데이터 모델을 업데이트하는 데 문제가 있습니다. wysiwyg edmx 데이터 모델링 편집기 (vs2010)에서 업데이트를했으며 업데이트가 정상적으로 보입니다."No such column Extent2. <fieldname>"문제

"SQLite는 에러 번호와 같은 열 : Extent2.Country_ID"

Country_ID가의 한 속성입니다하지만 내가 프로그램을 실행할 때 같이 이야기하기 어렵다는 데이터베이스에 액세스하려고 할 때, 즉시, 나는이 오류 기존 엔티티 (내가 수정하지 않은)이지만 "Extent2"가 무엇인지 전혀 알지 못합니다. 모든 관련 프로젝트 파일을 통해 철저한 텍스트 검색을 수행했는데 한 번 올리지 않았습니다. 예외 내의

는 TargetSite 읽 {System.Data.Common.DbDataReader ExecuteStoreCommands (System.Data.EntityClient.EntityCommand, System.Data.CommandBehavior는)}

안타깝게도 더 없기 정보; 오류 번호 또는 아무것도. 아이디어가 있으십니까?

감사

답변

2

Extent2는 엔티티 프레임 워크에 의해 생성 된 SQL에서 테이블 별명입니다. 생성 된 SQL 명령이 실제 데이터베이스 구조와 일치하지 않는 어딘가에 엔티티 모델의 잘못된 관계 또는 필드 매핑이있는 것 같습니다.

+0

아마 맞을지 모르겠지만, 슬프게도 wysiwyg edmx 편집기는 너무 끔찍해서 프레임 워크 내의 관계에 대해 걱정하지 않고 엔티티 만 추가하고 수동으로 관계를 처리했습니다. –

0

다음과 같이 (SQLite는의 엔티티 프레임 워크를 사용하는) 응용 프로그램이 테이블하지만 열이 누락 된 열을 감지하고 프로그램을 추가 할 수 없습니다있는 데이터베이스의 이전 버전을 여는 경우 : -

private EntityFrameworkEntities _modelInstance; 

    protected override void Load() 
    { 
     bool retry = false; 
     if (!TryLoad(out retry)) 
     { 
      if (retry) 
      { 
       AddColumnToTableInDatabase(); 
       TryLoad(out retry); 
      } 
     } 
    } 

    private bool TryLoad(out bool retry) 
    { 
     bool success = false; 
     retry = false; 
     using (_modelInstance = new EntityFrameworkEntities()) 
     { 
      _modelInstance.Connection.Open(); 

      var yourQuery = from entity in _modelInstance.Entitys 
           select entity; 
      try 
      { 
       foreach (Entity entity in yourQuery) 
       { 
        var vm = new EntityViewModel(entity, this); 
        base.Children.Add(vm); 
       } 
       success = true; 
      } 
      catch (Exception ex) 
      { 
       while (ex.InnerException != null) 
        ex = ex.InnerException; 
       if (ex.Message.ToLower().Contains("no such column") && ex.Message.Split(new char[] { '.' })[1].Equals("Country_ID")) 
        retry = true; 
       log.Error(ex.Message, ex); 
      } 
     } 
     return success; 
    } 

    private bool AddColumnToTableInDatabase() 
    { 
     bool success = false; 
     StringBuilder sql = new StringBuilder(@"ALTER TABLE [Entity] ADD COLUMN [Country_ID] [text] NULL"); 
     using (_modelInstance = new EntityFrameworkEntities()) 
     { 
      _modelInstance.Connection.Open(); 
      var connection = (_modelInstance.Connection as EntityConnection).StoreConnection as SQLiteConnection; 
      using (var transaction = connection.BeginTransaction()) 
      { 
       try 
       { 
        using (var command = connection.CreateCommand()) 
        { 
         command.CommandText = sql.ToString(); 
         command.ExecuteNonQuery(); 
        } 
        transaction.Commit(); 
        success = true; 
       } 
       catch (Exception ex) 
       { 
        log.Error(ex.Message, ex); 
        transaction.Rollback(); 
       } 
      } 
     } 
     return success; 
    } 
관련 문제