Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 创建唯一属性时出现主键异常

C# 创建唯一属性时出现主键异常,c#,entity-framework,asp.net-core,entity-framework-core,C#,Entity Framework,Asp.net Core,Entity Framework Core,我正在使用Asp.net核心和EF核心构建一个应用程序。我在执行代码优先的概念时遇到以下异常 “System.Reflection.CustomAttributeData”需要一个主键 定义 只有在DBContext类中使用isUnique定义modelbuilder时,才会发生这种情况。如果我注释掉整个modelbuilder,一切正常。它创建的数据库和表没有任何问题 下面是我的Modelbuilder: protected override void OnModelCreating(Mode

我正在使用Asp.net核心和EF核心构建一个应用程序。我在执行代码优先的概念时遇到以下异常

“System.Reflection.CustomAttributeData”需要一个主键 定义

只有在DBContext类中使用
isUnique
定义
modelbuilder
时,才会发生这种情况。如果我注释掉整个
modelbuilder
,一切正常。它创建的数据库和表没有任何问题

下面是我的
Modelbuilder

protected override void OnModelCreating(ModelBuilder modelbuilder)
    {
        // Make unique properties
        modelbuilder.Entity<Application>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<Domain>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<Roles>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<Environments>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<NetworkZone>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<Status>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<Type>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<OperatingSystems>()
            .HasIndex(a => a.OSVersion)
            .IsUnique();
        modelbuilder.Entity<Servers>()
            .HasIndex(s => s.ServerName)
            .IsUnique();
        modelbuilder.Entity<ResourceGroup>()
            .HasIndex(a => a.Name)
            .IsUnique();
        modelbuilder.Entity<AccessType>()
            .HasIndex(a => a.Name)
            .IsUnique();
    }

我通过将下面的模型生成器更改为正确的实体来修复它

发件人:

modelbuilder.Entity()
.HasIndex(a=>a.Name)
.IsUnique();
致:

modelbuilder.Entity()
.HasIndex(a=>a.Name)
.IsUnique();
系统使用了
类型
,因此在我的
modelbuilder

public abstract class EntityBase
{
    /// <summary>
    /// Gets or sets the identifier.
    /// </summary>
    /// <value>
    /// The identifier.
    /// </value>
    [Key]
    public int Id { get; set; }

    /// <summary>
    /// Gets or sets the created by.
    /// </summary>
    /// <value>
    /// The created by.
    /// </value>
    public string CreatedBy { get; set; }

    /// <summary>
    /// Gets or sets the create on.
    /// </summary>
    /// <value>
    /// The create on.
    /// </value>
    public DateTime CreatedOn { get; set; }

    /// <summary>
    /// Gets or sets the modified by.
    /// </summary>
    /// <value>
    /// The modified by.
    /// </value>        
    public string ModifiedBy { get; set; }

    /// <summary>
    /// Gets or sets the modified on.
    /// </summary>
    /// <value>
    /// The modified on.
    /// </value>
    public DateTime ModifiedOn { get; set; }
}
modelbuilder.Entity<Type>()
        .HasIndex(a => a.Name)
        .IsUnique();
modelbuilder.Entity<Types>()
        .HasIndex(a => a.Name)
        .IsUnique();