Entity Framework中的实体类添加复合主键
使用Code First模式实现给实体类添加复合主键,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace MyFirstMvcApp.Models
{
/// <summary>
/// 登录记录
/// </summary>
public class LoginRecordInfo
{
/// <summary>
/// 登录的邮件地址(主键)
/// </summary>
[Key,Column(Order=)]
public string Email { get; set; } /// <summary>
/// 登录的客户端IP
/// </summary>
public string LoginHostIP { get; set; } /// <summary>
/// 登录的客户端主机名
/// </summary>
public string LoginHostName { get; set; } /// <summary>
/// 登录时间(主键)
/// </summary>
[Key,Column(Order=)]
public DateTime LoginTime { get; set; }
}
}
使用特性Key和Column设置复合主键,Key表示字段是主键,Order用来设置主键的顺序。使用Key和Column需要添加命名空间:
Key的命名空间:System.ComponentModel.DataAnnotations;
Column的命名空间:System.ComponentModel.DataAnnotations.Schema;
Entity Framework中的实体类添加复合主键的更多相关文章
- Entity Framework 教程——Entity Framework中的实体类型
Entity Framework中的实体类型 : 在之前的章节中我们介绍过从已有的数据库中创建EDM,它包含数据库中每个表所对应的实体.在EF 5.0/6.0中,存在POCO 实体和动态代理实体两种. ...
- 如何给Sqlite添加复合主键
如果是想两个字段组成一个复合主键的话可以如下.SQL code sqlite> create table t2 ( ...> id1 int , ...> id2 int, ...& ...
- sql语句,数据库中,同表添加,主键不同,数据相同。
insert into tablename(各个字段名) select #新主键值,其他字段名 from tablename where No = #表中主键值
- Entity framework 中Where、First、Count等查询函数使用时要注意
在.Net开发中,Entity framework是微软ORM架构的最佳官方工具.我们可以使用Lambda表达式在Entity framework中DbSet<T>类上直接做查询(比如使用 ...
- 【hibernate/JPA】注解方式实现 复合主键【spring boot】
1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...
- hibernate复合主键
需要用到实体类Role的主键和Menu的主键结合起来作为实体类RoleMenu的主键,那么通过Hibernate具体实现如下: RoleMenu实体类:(注意该实体类需要实现Serializable接 ...
- 【Hibernate步步为营】--复合主键映射具体解释
上篇文章讨论了继承映射,它是对象模型中最主要的特性,对于继承映射它的主要区分是字段类型的不同,所以在生成表结构时须要有新列来标识数据的类型,能够使用<subclass>标签并在标签中加入d ...
- Entity Framework中的几种加载方式
在Entity Framework中有三种加载的方式,分别是延迟加载,自动加载和显示加载.下面用一个例子来说明:现在有两个表,一个是资料表(Reference),另外一个表是资料分类表 ...
- Lazy<T>在Entity Framework中的性能优化实践
Lazy<T>在Entity Framework中的性能优化实践(附源码) 2013-10-27 18:12 by JustRun, 328 阅读, 4 评论, 收藏, 编辑 在使用EF的 ...
随机推荐
- UI设计经常使用站点
前期:http://www.zcool.com.cn/ 中期:http://www.ui.cn/ 后期:https://dribbble.com/
- (剑指Offer)面试题54:表示数值的字符串
题目: 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3.14 ...
- 【PAT】1028. List Sorting (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028 题目描述: Excel can sort records according to an ...
- volist 自增序号 分页如何实现?
TP框架模板中如何生成自增数据 {$_GET['p']*10-10+$i} /* 分页序号计算 */ function addnum($k,$num){ return ($k +1 ) ...
- js 不固定传参
js 不固定传参 CreationTime--2018年7月2日15点18分 Author:Marydon /** * 声明一个函数 * @explain 传参个数不确定 */ function ...
- oracle 存储过程 示例
oracle 存储过程 示例 CreationTime--2018年9月4日09点49分 Author:Marydon 1.情景展示 对VIRTUAL_QRCODELOG表的静态二维码,动态二维码 ...
- 公用的css
/*公共开始*/ body, ol, ul, h1, h2, h3, h4, h5, h6, p, th, td, dl, dd, form, fieldset, legend, input, tex ...
- Android异步下载
概述 实现App常见下载公共 支持通知栏显示 支持 暂停.取消功能,使用Service.AsyncTask实现异步下载.特点简单.实用.方便源码扩展修改 详细 代码下载:http://www.demo ...
- Android:EditText限制文字输入
Android的编辑框控件EditText在平常编程时会经常用到,有时候会对编辑框增加某些限制,如限制只能输入数字,最大输入的文字个数,不能输入 一些非法字符等,这些需求有些可以使用android控件 ...
- mysql create table 语法详解
create table 可以分成三类 一.一般create table 语句: 1 语法 create [temporary] table [if not exists] tbl_name (cre ...