2013-05-28 34 views
0

我已經繼承了一個數據庫 - 它具有「ref」和「2parq」作爲字段。對模型中使用的字的ASP.Net MVC限制

當建立一個模型來表示這個數據庫中的表,我得到的錯誤:

Invalid token 'ref' in class, struct, or interface member declaration

我的類如下:

public class Call 
{ 
    [Key] 
    public int CallId { get; set; } 
    public string ref { get; set; } 

重命名數據庫字段是不是很遺憾的選項。

無論如何,我可以參考我的模型中的「ref」和「2parq」字段嗎?

謝謝,馬克

回答

2

嘗試

public string @ref { get; set; } 

@符號將告訴編譯器,它應忽略保留關鍵字ref,並把它當作一個普通變量。

MSDN文檔可以找到here

1

前面加上@符號屬性名稱:

public class Call 
{ 
    [Key] 
    public int CallId { get; set; } 
    public string @ref { get; set; } 

MSDN

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

+0

謝謝你 - 這對於「裁判」欄目工作,但不是「2parq」一欄,如果我更改爲「@ 2parq」 - 我仍然得到錯誤:類型或命名空間「parq」不能被發現 - 謝謝,馬克 – Mark

+0

沒有跟進這一個,對不起。在C#中,你不能有以數字開頭的標識符 - 所以你必須使用devdigital的建議(儘管我仍然不確定它是否適用於以數字開頭的列名)。 –

5

而不是使用@ref,我會建議使用正確的PascalCase屬性名稱,並使用修改映射或者Column屬性,或更好的流利的映射API。

[Column(Name="ref")] 
public string Reference { get; set; }