2015-10-20 41 views

回答

3

沒有從字符串到字節數組的直接轉換。您必須指定字節數組以什麼編碼表示字符串。

可以添加非映射包裝特性(下面TheStringValue),其使用特定的編碼字符串的字節和副映射反之亦然:

public class MyEntity 
{ 
    public byte[] StringBytes { get; set; } 

    [NotMapped] 
    public string TheStringValue 
    { 
     get 
     { 
      return Encoding.UTF8.GetString(StringBytes); 
     } 
     set 
     { 
      StringBytes = Encoding.UTF8.GetBytes(value); 
     }  
    } 
} 
+0

'[NotMapped]' - NotMapped屬性可以被應用於一類的屬性。默認的Code-First約定爲包含getter和setter的所有屬性創建一個列。 NotMapped屬性重寫此默認約定。您可以將NotMapped屬性應用於您不希望在數據庫表中創建列的屬性。 [更多信息](http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx) –

相關問題