3

I'm trying to code a WCF RIA class for the first time in C# (also my first time with the language), just to join some tables for a LightSwitch data source. Following the example code, I created the following class to return my rows of data:

public class StudentTerm
{
    [Key]
    public string TermCode { get; set; }
    public string StudentID { get; set; }
    public decimal? TermGPA { get; set; }
    public decimal? CumulativeGPA { get; set; }
}

But I'm curious -- what is the purpose of the [Key] typedef? Does it apply only to TermCode in my example, or to the entire class?

secretformula
  • 6,414
  • 3
  • 33
  • 56
Henson
  • 33
  • 1
  • 3
  • That's your primary key - http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.keyattribute(v=vs.110).aspx – Darren Wainwright Jun 18 '14 at 14:18
  • Because you probably have a table `StudentTerm` in your database and `TermCode` is the primary key in table. – Habib Jun 18 '14 at 14:18
  • It's called an attribute and it only applies to `TermCode` in your example. I'm not familiar with the library to know what it represents. – Daniel Kelley Jun 18 '14 at 14:18
  • @Darren So it's an attribute, setting TermCode as the key? Only `StudentID` would be unique, so I guess I need to edit the class. – Henson Jun 18 '14 at 14:20
  • `[Key]` is an attribute. Attributes only apply to the thing they're right before, so it's only on `TermCode`; if it were on the whole class it'd be `[Key] public class StudentTerm` – Tim S. Jun 18 '14 at 14:23
  • Yes, it's an attribute. It's likely you need to edit the class yes; especially if there are not DISTINCT `TermCode`. You will also likely need to change the `StudentID` type too - though don't know your business logic. If your table is going to contain a LOT of data then string as a PK will likely suffer performance. – Darren Wainwright Jun 18 '14 at 14:23
  • And the way I see it, this is a transition table between Student and Term? You can define a composite key with the TermCode and StudentId properties by using the [Column(Order = 0), Key] attribute. Use Order = 0 for first part of key, and Order = 1 for second part. – Edwin van Vliet Jun 18 '14 at 14:25

3 Answers3

2

As @LIUFA says above, Key is an Attribute. It's full name is System.ComponentModel.DataAnnotations.KeyAttribute This is to denote that the Id field is the primary key of the underlying data store.

In a more general sense, an Attribute can decorate a class, method, property, or assembly. They don't do anything on their own, but are used by other code to "mark" things.

David Crowell
  • 3,711
  • 21
  • 28
1

Key on top of TermCode is called property attribute.
Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.
More on attributes you can find here.

In this speciffic case we are talking about WCF RIA Services attribute.
In WCF RIA Services each entity must have a primary key defined and you designate a property as the primary key with the [Key] attribute. You can decorate multiple properties if the class has a composite primary key.

Community
  • 1
  • 1
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
0

You should probably check the msdn libraries for an answer: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.keyattribute(v=vs.110).aspx

And yes, it only applies to TermCode. Class attributes are placed just above the class definition. Also Key Is a property of field attribute so it can't be applied to a class

memory of a dream
  • 1,207
  • 3
  • 23
  • 32