Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架主键异常_C#_Entity Framework - Fatal编程技术网

C# 实体框架主键异常

C# 实体框架主键异常,c#,entity-framework,C#,Entity Framework,我有一个非常简单的C#类,它包含一个主键。我首先使用EF 6和代码,我有两个问题: 如何首先在代码中将主键设置为标识(以及如何不将其设置为标识)?我的一些类需要PK作为一个标识(加法时自动递增),还有一些类需要它由代码分配 当我尝试使用以下命令将类保存到数据库时: // This code is actually wrapped by a BusinessContext Class to encapsulate the ORM representation context.Costumers.A

我有一个非常简单的C#类,它包含一个主键。我首先使用EF 6和代码,我有两个问题:

  • 如何首先在代码中将主键设置为
    标识
    (以及如何不将其设置为标识)?我的一些类需要PK作为一个标识(加法时自动递增),还有一些类需要它由代码分配

  • 当我尝试使用以下命令将类保存到数据库时:

    // This code is actually wrapped by a BusinessContext Class to encapsulate the ORM representation
    context.Costumers.Add(costumer); //exception is here!
    context.SaveChanges();
    
    我的类在
    Add
    方法上得到以下异常:

    在模型生成过程中检测到一个或多个验证错误:

    M.Customer::EntityType“Customer”未定义键。定义此EntityType的键。
    Customers:EntityType:EntitySet“Customers”基于没有定义键的类型“Customer”

  • 这就是课堂本身:

    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; }            
            } 
        }
    }
    

    类确实有一个
    [Key]
    ,那么为什么会有一个异常说没有键呢?

    实体框架必须为任何
    [Key]
    属性都有一个getter和setter

    如果要指定EF生成此字段的值,可以使用
    [DatabaseGeneratedOption.None]

    枚举有3个不同的值:

    • Computed——插入或更新行时,数据库生成一个值
    • 标识——插入行时,数据库生成一个值
    • 无--数据库不生成值

    键值的默认值是DatabaseGeneratedOption。对于任何
    [key]
    属性,Identity框架必须具有getter和setter

    如果要指定EF生成此字段的值,可以使用
    [DatabaseGeneratedOption.None]

    枚举有3个不同的值:

    • Computed——插入或更新行时,数据库生成一个值
    • 标识——插入行时,数据库生成一个值
    • 无--数据库不生成值

    键值的默认值是
    DatabaseGeneratedOption.Identity

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

    使用[DatabaseGeneratedOption.None]属性。a
    Key
    不能有私有setter。a
    Key
    不能有私有setter。但在这个特定类中,我希望DB使用标识自动生成ID…因此我删除了setter,既然没有能力设置数据库应该设置的内容就没有多大意义了……我如何将其配置为标识?那么,如果连Microsoft都将标识设置为默认设置,为什么还要强制您设置一个setter呢?(这意味着该集合无论如何都将被忽略)setter不会被忽略,它由EF在内部使用,从insert的返回设置实体中的值。它还用于在您从数据库中进行查找时填充值。但是在这个特定的类中,我希望DB使用标识自动生成ID…因此我删除了setter,因为它无法设置DB应该设置的内容…如何将其配置为标识?因此,为什么微软强迫你有一个setter,即使他们将Identity作为默认设置?(这意味着该集合无论如何都将被忽略)setter不会被忽略,它由EF在内部使用,从insert的返回设置实体中的值。当您从数据库进行查找时,它还用于填充值。