首先使用代码禁用整数主键的标识

首先使用代码禁用整数主键的标识

本文介绍了首先使用代码禁用整数主键的标识(自动递增)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP.NET MVC 3 应用程序中使用代码优先方法,并且模型中的所有整数主键 (public int Id { get; set; }) 默认配置为具有自动递增.如何禁用此功能并启用手动输入主键整数的方法?

I am using code first approach in a ASP.NET MVC 3 application and all integer primary keys in models (public int Id { get; set; }) are by default configured as an identity with auto-incrementing. How to disable this and enable a way to manually enter the integer for the primary key?

实际情况是 Id 整数具有特殊含义,因此我希望它们在创建时可以选择并在以后可编辑.如果在创建时没有给出整数,它会自动递增,否则使用指定的值,这将是理想的.但可编辑的主要字段是我的主要需求.有没有办法在 ASP.NET MVC 3 中优雅地做到这一点?

The actual situation is that the Id integers have a special meaning and I would therefore like to have them choosable at creation and later editable. It would be ideal if in case the integer is not given at creation time it is auto-incremented, else the specified value is used. But editable primary fields is my primary need. Is there any way to do this elegantly in ASP.NET MVC 3?

推荐答案

使用这些数据注释选项:

Use these data annotation options:

  • [System.ComponentModel.DataAnnotations.KeyAttribute()]
  • [System.ComponentModel.DataAnnotations.DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]

这篇关于首先使用代码禁用整数主键的标识(自动递增)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:09