简体   繁体   English

实体框架主键异常

[英]Entity Framework Primary Key Exception

I have a very simple C# class, which contains one primary key. 我有一个非常简单的C#类,其中包含一个主键。 I am using EF 6 with code-first and I have two questions: 我正在使用代码优先的EF 6,但有两个问题:

  1. How do I make a primary key as an Identity in code-first (and how to not make it an identity)? 如何在代码优先的情况下使主键作为Identity (以及如何不使其成为身份)? Some of my classes need the PK to be an identity (auto increment on addition) and some need it to be assigned by code. 我的某些班级需要PK作为身份(添加时自动递增),而某些班级则需要通过代码分配它。

  2. When I try to save the class to the database using: 当我尝试使用以下方法将类保存到数据库时:

     // This code is actually wrapped by a BusinessContext Class to encapsulate the ORM representation context.Costumers.Add(costumer); //exception is here! context.SaveChanges(); 

    I get the following exception for my class, on the Add method: 我的类在Add方法上收到以下异常:

    One or more validation errors were detected during model generation: 在模型生成期间检测到一个或多个验证错误:

    M.Costumer: : EntityType 'Costumer' has no key defined. M.Costumer :: EntityType'Costumer'尚未定义键。 Define the key for this EntityType. 定义此EntityType的键。
    Costumers: EntityType: EntitySet 'Costumers' is based on type 'Costumer' that has no keys defined. Costumers:EntityType:EntitySet'Costumers'基于未定义键的'Costumer'类型。

and this is the class itself: 这是类本身:

using Microsoft.Practices.Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApp.Models
{
    public class Costumer : BindableBase
    {
        private string name;
        private string city;
        private Estado state;
        private int costumerId;

        [Required]
        [StringLength(500)]        
        public string Name
        {
            get { return this.name; }
            set { SetProperty(ref name, value); }
        }

        [Required]
        [StringLength(500)]
        public string City
        {
            get { return this.city; }
            set { SetProperty(ref city, value); }
        }

        [Required]
        public State State
        {
            get { return this.state; }
            set { SetProperty(ref state, value); }
        }

        [Required]
        [Key]
        public int CostumerId
        {
            get { return this.costumerId; }            
        } 
    }
}

The class does have a [Key] , so how come there is an exception saying there isn't a key? 该类确实具有[Key] ,那么为什么会有一个异常声明没有键呢?

Entity Framework must have a getter and a setter for any [Key] attributes. 实体框架必须具有用于[Key]属性的getter和setter。

If you want to designate to EF that you will be generating the values for this field, you can use [DatabaseGeneratedOption.None] . 如果要指定EF,则将为此字段生成值,则可以使用[DatabaseGeneratedOption.None]

the DatabaseGeneratedOption Enumeration has 3 different values: DatabaseGeneratedOption枚举具有3个不同的值:

  • Computed -- The database generates a value when a row is inserted or updated. 已计算-插入或更新行时,数据库会生成一个值。
  • Identity -- The database generates a value when a row is inserted. 身份-插入行时数据库生成一个值。
  • None -- The database does not generate values. 无-数据库不生成值。

The default for a key value is DatabaseGeneratedOption.Identity 键值的默认值为DatabaseGeneratedOption.Identity

在密钥中使用[DatabaseGeneratedOption.None]属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM