Tuesday, June 3, 2014

How to set primary key in entity framework

11:24 PMTuesday, June 3, 2014
Suppose you have a class product, which is work as a table in entity framework. In this class you want to create a primary key. This features provides by System.ComponentModel.DataAnnotations namespace. According to msdn article this namespace provides attributes classes that are used to define metadata. In this example we are creating one or more columns , which is uniquely identified in the table. Suppose, in this table you want to set ProductId as a primary key then simple write 'Key' in brackets before fields. Lets a class
How to set primary key in entity framework

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// Summary description for Product
/// </summary>
public class Product
    public int ProductId { get; set; } // Primary Key attribute
    public string  productName { get; set; }
Protected by Copyscape Online Copyright Protection Software


Post a Comment

Toggle Footer