zoukankan      html  css  js  c++  java
  • Entity Framework中的实体类添加复合主键

    使用Code First模式实现给实体类添加复合主键,代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel.DataAnnotations;
     4 using System.ComponentModel.DataAnnotations.Schema;
     5 using System.Linq;
     6 using System.Web;
     7 
     8 namespace MyFirstMvcApp.Models
     9 {
    10     /// <summary>
    11     /// 登录记录
    12     /// </summary>
    13     public class LoginRecordInfo
    14     {
    15         /// <summary>
    16         /// 登录的邮件地址(主键)
    17         /// </summary>
    18         [Key,Column(Order=1)]
    19         public string Email { get; set; }
    20 
    21         /// <summary>
    22         /// 登录的客户端IP
    23         /// </summary>
    24         public string LoginHostIP { get; set; }
    25 
    26         /// <summary>
    27         /// 登录的客户端主机名
    28         /// </summary>
    29         public string LoginHostName { get; set; }
    30 
    31         /// <summary>
    32         /// 登录时间(主键)
    33         /// </summary>
    34         [Key,Column(Order=2)]
    35         public DateTime LoginTime { get; set; }
    36     }
    37 }

    使用特性Key和Column设置复合主键,Key表示字段是主键,Order用来设置主键的顺序。使用Key和Column需要添加命名空间:
    Key的命名空间:System.ComponentModel.DataAnnotations;
    Column的命名空间:System.ComponentModel.DataAnnotations.Schema;

  • 相关阅读:
    查找->静态查找表->次优查找(静态树表)
    P1993-小K的农场
    P1983-车站分级
    P1268-树的重量
    P1113-杂务
    P1265-公路修建
    P2330-[SCOI2005]繁忙的都市
    P1546-最短网络
    P1144-最短路计数
    P1462-通往奥格瑞玛的道路
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/7275510.html
Copyright © 2011-2022 走看看