学无先后,达者为师

网站首页 编程语言 正文

EF使用Code First模式给实体类添加复合主键_实用技巧

作者:.NET开发菜鸟   更新时间: 2022-05-03 编程语言
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace MyFirstMvcApp.Models
{
    /// 
    /// 登录记录
    /// 
    public class LoginRecordInfo
    {
        /// 
        /// 登录的邮件地址(主键)
        /// 
        [Key,Column(Order=1)]
        public string Email { get; set; }

        /// 
        /// 登录的客户端IP
        /// 
        public string LoginHostIP { get; set; }

        /// 
        /// 登录的客户端主机名
        /// 
        public string LoginHostName { get; set; }

        /// 
        /// 登录时间(主键)
        /// 
        [Key,Column(Order=2)]
        public DateTime LoginTime { get; set; }
    }
}

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

  • Key的命名空间:System.ComponentModel.DataAnnotations;
  • Column的命名空间:System.ComponentModel.DataAnnotations.Schema;

原文链接:https://www.cnblogs.com/dotnet261010/p/7275510.html

栏目分类
最近更新