2016-12-16 39 views
0

嗯,我試圖實施只是a tutorial by Milinaudara所解釋的,因爲它是第一個谷歌打我發現EF 6自定義註解。該教程是非常容易。但是,它看起來像我將需要添加[CaseSensitive]註解某一列後,我遇到的第一個遷移 - creating table - 和[CaseSensitive]將在第二個遷移,爲alter table操作實際執行。我錯了嗎?因爲這是我到目前爲止已經經歷了..實體框架定義註解

是否有可能爲註釋在創建表上運行?我應該重寫EF的Generate(CreateTableOperation createTableOperation)方法,以確保[CaseSensitive]註釋實際得到應用?

回答

0

Generate(AlterColumnOperation alterColumnOperation)只有在模型發生變化時才被調用,並且永遠不會在表格創建或添加新列時被觸發 - 也許這很明顯,但教程沒有提及它..每個方法的背景故事至少會更好..我最終覆蓋Generate(AddColumnOperation addColumnOperation)Generate(CreateTableOperation createTableOperation)所以它看起來像:

protected override void Generate(AddColumnOperation addColumnOperation) 
    { 
     //add the column 
     base.Generate(addColumnOperation); 

     //alter the column 
     this.Generate(new AlterColumnOperation(addColumnOperation.Table, addColumnOperation.Column, false)); 
    } 

    protected override void Generate(CreateTableOperation createTableOperation) 
    { 
     //add the column 
     base.Generate(createTableOperation); 

     //alter the column 
     foreach(ColumnModel column in createTableOperation.Columns) 
     { 
      this.Generate(new AlterColumnOperation(createTableOperation.Name, column, false)); 
     } 
    } 

雖然我知道不應該承擔所有的改變操作是安全的..所以,路過false當構建AlterColumnOperation可能是一個壞主意。