类型中的属性名称必须是唯一的

类型中的属性名称必须是唯一的

本文介绍了类型中的属性名称必须是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 5,我有以下实体:

  public class User {
public Int32 Id {get;组; }
public String Username {get;组; }
public virtual ICollection< CLaim> CLaims {get;组; }
}

public class Claim {
public Int32 Id {get;组; }
public String Type {get;组; }
public String Value {get;组; }
public virtual User User {get;组;
}

有关这些实体的几个注释:


  1. 在用户实体中,Id是PK;

  2. 在Claim实体中,Id是FK,等于User.Id;

  3. 在索赔实体中,PK是从(Id,Type,Value)复合到$ /

    我有这些实体的以下SQL:

     创建表dbo.Users 

    Id int identity不为空
    约束PK_Users_Id主键集群(Id),
    用户名nvarchar(120)not null
    约束UQ_Users_Username unique(Username)
    );

    创建表dbo.Claims

    Id int not null,
    [Type] nvarchar(200)not null,
    值nvarchar(200)不为空,
    约束PK_Claims_Id_Type_Value主键(Id,[Type],Value),
    );

    alter table dbo.Claims
    添加约束FK_CLaims_Id外键(Id)
    引用删除级联上的dbo.Users(Id)更新级联;

    最后,实体的配置如下:

     内部类UserConfiguration:EntityTypeConfiguration< User> {
    内部UserConfiguration():base(){
    ToTable(Users);
    HasKey(x => x.Id);
    属性(x => x.Id).IsRequired()。HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    属性(x => x.Username).IsRequired()。HasMaxLength(120);
    }
    }

    内部类ClaimConfiguration:EntityTypeConfiguration< Claim> {
    internal ClaimMapper():base(){
    ToTable(Claims);
    HasKey(x => new {x.Id,x.Type,x.Value});
    属性(x => x.Id).IsRequired()。HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    属性(x => x.Type).IsRequired()。HasMaxLength(200);
    属性(x => x.Value).IsRequired()。HasMaxLength(200);

    HasRequired< User>(x => x.User).WithMany(y => y.Claims).Map(z => {z.MapKey(Id);} );
    }
    }

    问题: / p>

    当我尝试创建一个用户时,我收到以下错误:

    有谁知道我可能做错了什么?


    只有当您的外键列未作为模型中的属性公开时,才会使用MapKey

    解决方案

    但在你的情况下,它是 - 作为属性 Claim.Id 。在这种情况下,您必须使用 HasForeignKey 而不是 MapKey

      HasRequired< User>(x => x.User)
    .WithMany(y => y.Claims)
    .HasForeignKey(x => x.Id);


    I am using Entity Framework 5 and I have the following entities:

    public class User {
      public Int32 Id { get; set; }
      public String Username { get; set; }
      public virtual ICollection<CLaim> CLaims { get; set; }
    }
    
    public class Claim {
      public Int32 Id { get; set; }
      public String Type { get; set; }
      public String Value { get; set; }
      public virtual User User { get; set; }
    }
    

    A few notes about these entities:

    1. In User entity the Id is the PK;
    2. In Claim entity the Id is a FK and is equal to User.Id;
    3. In Claim entity the PK is composite from (Id, Type, Value)

    So I have the following SQL for these entities:

    create table dbo.Users
    (
      Id int identity not null
        constraint PK_Users_Id primary key clustered (Id),
      Username nvarchar (120) not null
        constraint UQ_Users_Username unique (Username)
    );
    
    create table dbo.Claims
    (
      Id int not null,
      [Type] nvarchar (200) not null,
      Value nvarchar (200) not null,
        constraint PK_Claims_Id_Type_Value primary key (Id, [Type], Value),
    );
    
    alter table dbo.Claims
    add constraint FK_CLaims_Id foreign key (Id)
        references dbo.Users(Id) on delete cascade on update cascade;
    

    Finally, the configuration of the entities are as follows:

    internal class UserConfiguration : EntityTypeConfiguration<User> {
      internal UserConfiguration() : base() {
        ToTable("Users");
        HasKey(x => x.Id);
        Property(x => x.Id).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.Username).IsRequired().HasMaxLength(120);
      }
    }
    
    internal class ClaimConfiguration : EntityTypeConfiguration<Claim> {
      internal ClaimMapper() : base() {
        ToTable("Claims");
        HasKey(x => new { x.Id, x.Type, x.Value });
        Property(x => x.Id).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        Property(x => x.Type).IsRequired().HasMaxLength(200);
        Property(x => x.Value).IsRequired().HasMaxLength(200);
    
        HasRequired<User>(x => x.User).WithMany(y => y.Claims).Map(z => { z.MapKey("Id"); });
      }
    }
    

    THE PROBLEM:

    When I try to create a user I get the following error:

    Does anyone knows what I might be doing wrong?

    解决方案

    MapKey is only used if your foreign key column is not exposed as a property in your model. But in your case it is - as property Claim.Id. In that case you must use HasForeignKey instead of MapKey:

    HasRequired<User>(x => x.User)
        .WithMany(y => y.Claims)
        .HasForeignKey(x => x.Id);
    

    这篇关于类型中的属性名称必须是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:12