0

I have inherited a database - which has "ref" and "2parq" as fields.

When building a model to represent this database table, I get the error:

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

My Class is below:

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

Renaming the database field isn't an option unfortunately.

Is there anyway I can reference the "ref" and "2parq" fields within my model?

Thanks, Mark

4

3 回答 3

5

Instead of using @ref, I would recommend using a correct PascalCase property name, and change the mapping using either the Column attribute, or better still the fluent mapping API.

[Column(Name="ref")]
public string Reference { get; set; }
于 2013-05-28T10:00:09.337 回答
2

Try

public string @ref { get; set; }

@ symbol will tell the compiler, it should ignore the reserved keyword ref and treat it as a regular variable.

MSDN documentation can be found here.

于 2013-05-28T09:57:41.937 回答
1

Prepend @ symbol to the property name:

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

From 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.

于 2013-05-28T09:58:03.033 回答