4

정말 이상한 문제에 직면했습니다. 자식 프로젝트에서 마이그레이션 할 콘솔 앱이 있습니다. 내가 같이 cmd를에서 실행하면내가 EF 코드를 실행할 때 콘솔 입력 문자열이 올바른 형식 오류가 아닙니다.

  Sql(
      "INSERT [Script] ([Id], [CompanyId], [ScriptType], [ScriptCode], [Enable], [Schema]) VALUES (38500, 0, 3, N'if(Row.OccupancyCode == \"8500\") Row.MSBOccupancyId = 8500;', 1, N'SQA'), " + 
      "(38510, 0, 3, N'if(Row.OccupancyCode == \"8510\") Row.MSBOccupancyId = 8510;', 1, N'SQA'), " + 
      "(38535, 0, 3, N'if(Row.OccupancyCode == \"8535\") Row.MSBOccupancyId = 8535;', 1, N'SQA'); "); 

: 나는 닷넷 4, EF 5. 여기에 사용하는 것은 내 마이그레이션입니다

migrate.exe MSB.PI.Data.PortfolioInsight.dll /connectionString="Data Source=SERVER;User=USER;Password=PASSWORD;Initial Catalog=AAA;" /connectionProviderName="System.Data.SqlClient" /verbose

나는 나타납니다

VERBOSE: Target database is: 'AAA' (DataSource: evbyminsd1144, Provider: System.Data.SqlClient, Origin: Explicit). Applying code-based migrations: [201304161824179_InsertBaseDataScripts]. Applying code-based migration: 201304161824179_InsertBaseDataScripts. System.Data.Entity.Migrations.Design.ToolingException: Input string was not in a correct format. at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.Console.Program.Run() at System.Data.Entity.Migrations.Console.Program.Main(String[] args) ERROR: Input string was not in a correct format.

그러나 모든 진술이 정확합니다.

무엇이 잘못 되었나요? 제발 도와주세요.

미리 감사드립니다.

+0

TeamCity에 빌드 프로세스를 통합하는 동안 같은 문제가 발생합니다. @ James-Morcom이 말했듯이 이것은 migrate.exe에서 실행할 때만 문제가됩니다. 패키지 관리자 콘솔에서는 모든 것이 잘된 것처럼 보입니다. –

답변

0

마이그레이션에서 원시 SQL 내에서 문자열을 구문 분석하는 것과 비슷한 문제가있었습니다.

Sql("SELECT '{'"); 

패키지 관리자 콘솔에서 작업을하지만, 그래서이 될 것처럼 보인다 않습니다 마이그레이션 내의 다음 유효한 SQL은 migrate.exe에서 실행 "입력 문자열이 올바른 형식이 아니었다"던졌습니다 migrate.exe와 관련된 문제

N.b. 나는 4.3.1을 사용하고 있었다.

하나의 옵션은 "-Script"매개 변수를 사용하여 비주얼 스튜디오 내에서 패키지 관리자 콘솔에서 마이그레이션을 실행할 수 있습니다 :이 마이그레이션 사용하지 않고 실행하는 SQL 스크립트를 생성 할 수 있습니다 것

Update-Database -Script -SourceMigration:"MigrationBeforeProblemOne" -TargetMigration:"ProblemMigration" 

. exe인데, 데이터베이스로의 마이그레이션 만 수행하면됩니다 (migrate.exe가 나중에이 특정 마이그레이션을 건너 뛸 수 있음).

1

해킹 경고

내가이 문제를 해결하기 위해 해킹을 추가 할 필요가 밝혀졌습니다.

migrate.exe가 String.Format()을 어딘가에서 수행하려고하기 때문에 문제가 발생할 가능성이 큽니다.

해킹 :

  1. 유니 코드
  2. 를 사용하여 DbMigration 클래스와 "새"를 Sql() 기능 그래서 우리는 '{'와 '}' System.Data.Entity.Migrations.DbMigration.Sql()
  3. 탈출에 전달하기 전에 원시 SQL을 탈출 할 수 만들기

코드 :

// This is a hack for: 
//  https://stackoverflow.com/questions/16044838/when-i-run-ef-code-migration-from-console-input-string-was-not-in-a-correct-fo 
// Without it migrate.exe will not work if you try to run Sql("Select '{'") 
// 
// This is most likely because migrate.exe is trying to do String.Format() somewhere 
public abstract class DbMigration : System.Data.Entity.Migrations.DbMigration 
{ 
    protected internal new void Sql(string sql, bool suppressTransaction = false, object anonymousArguments = null) 
    { 
     base.Sql(Escape(sql), suppressTransaction, anonymousArguments); 
    } 

    // EF migrate.exe does not support any script with the character '{' or '}' 
    // Encode them into unicode and add the strings on Sql server side 
    private string Escape(string s) 
    { 
     return s.Replace("{", "'+NChar(123)+'").Replace("}", "'+NChar(125)+'"); 
    } 
} 

것들 t hat 시도했지만 작동하지 않았습니다.

  1. EntityFramework.psm1을 가져 와서 PowerShell에서 update-database를 실행합니다. The name of the current PowerShell host is 'ConsoleHost'이라는 메시지가 나타납니다. update-database은 NuGet의 패키지 관리자 콘솔에서 많은 방법을 사용하기 때문입니다.Read more here
  2. 패키지 관리자 콘솔을 powershell에서 실행하십시오. 제안 된대로 가능하지 않을 가능성이 높습니다. here
  3. EntityFramework.psm1에 문제가없는 이유를 파악하고 해당 솔루션을 사용하려고합니다. 이 스크립트에는 500 줄의 코드가 포함되어 있으며 실제로 migrate.exe를 실행하는 것 이상의 기능을 수행합니다. 해킹이 더 쉬워 보인다.
관련 문제