简体   繁体   English

EF单向一对多关系

[英]EF Unidirectional One to Many relationship

I have an IdentityUser class which has a navigation property called Logins which is of type Collection . 我有一个IdentityUser类,该类具有一个名为Logins的导航属性,该属性的类型为Collection

public class IdentityUser : IUser
{
    public IdentityUser();
    public IdentityUser(string userName);

    public virtual ICollection<IdentityUserClaim> Claims { get; }
    public virtual string Id { get; set; }
    public virtual ICollection<IdentityUserLogin> Logins { get; }
    public virtual string PasswordHash { get; set; }
    public virtual ICollection<IdentityUserRole> Roles { get; }
    public virtual string SecurityStamp { get; set; }
    public virtual string UserName { get; set; }
}

In the IdentityUserLogin class there is a navigation property called User which is of type IdentityUser . IdentityUserLogin类中,有一个名为User的导航属性,其类型为IdentityUser

public class IdentityUserLogin
{
    public IdentityUserLogin();

    public virtual string LoginProvider { get; set; }
    public virtual string ProviderKey { get; set; }
    public virtual IdentityUser User { get; set; }
    public virtual string UserId { get; set; }
}

From this I can see that 从这个我可以看到

  1. the IdentityUser can have 0 to many IdentityUserLogin . IdentityUser可以具有0到许多IdentityUserLogin
  2. IdentityUserLogin must have a IdentityUser . IdentityUserLogin必须具有一个IdentityUser

I tried: 我试过了:

        this.HasRequired(t => t.User)
            .WithRequiredPrincipal();

but it was looking for a foreign key of IdentityUser_Id but it should be using UserId (which is a property of IdentityUserLogin 但它正在寻找IdentityUser_Id的外键,但应该使用UserId (这是IdentityUserLogin的属性

so then I tried: 所以我尝试了:

        this.HasRequired(t => t.User)
            .WithMany(t => t.Logins)
            .HasForeignKey(t => t.UserId);

but I get an error stating: 但我得到一个错误说明:

IdentityUserLogin_User_Source: : Multiplicity is not valid in Role 'IdentityUserLogin_User_Source' in relationship 'IdentityUserLogin_User'. IdentityUserLogin_User_Source::多重性在关系“ IdentityUserLogin_User”中的角色“ IdentityUserLogin_User_Source”中无效。 Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'. 由于从属角色指的是关键属性,因此从属角色的多重性上限必须为“ 1”。

Does anyone know how I can declare the relationship and specify the Foreign Key? 有谁知道我如何声明该关系并指定外键?

Update 1 更新1

This is my IdentityUserLogin map class: 这是我的IdentityUserLogin映射类:

public class IdentityUserLoginMap : EntityTypeConfiguration<IdentityUserLogin>
{
    public IdentityUserLoginMap()
    {
        // Primary Key
        this.HasKey(t => t.UserId);

        // Properties
        this.Property(t => t.LoginProvider)
            .IsRequired()
            .HasMaxLength(128);

        this.Property(t => t.ProviderKey)
            .IsRequired()
            .HasMaxLength(128);

        this.Property(t => t.UserId)
            .IsRequired()
            .HasMaxLength(128);

        // Table & Column Mappings
        this.ToTable("IdentityUserLogins");
        this.Property(t => t.LoginProvider).HasColumnName("LoginProvider");
        this.Property(t => t.ProviderKey).HasColumnName("ProviderKey");
        this.Property(t => t.UserId).HasColumnName("UserId");

        // Relationships
        this.HasRequired(t => t.User)
            .WithRequiredPrincipal();
    }
}

As you can see, I am setting a primary key 如您所见,我正在设置主键

Update 2 更新2

Just to give a bit more of an explanation, here is the users table: 这里只提供更多说明,这是users表:

CREATE TABLE [dbo].[IdentityUsers](
    [Id] [nvarchar](128) NOT NULL,
    [UserName] [nvarchar](max) NULL,
    [PasswordHash] [nvarchar](max) NULL,
    [SecurityStamp] [nvarchar](max) NULL,
    [Discriminator] [nvarchar](128) NOT NULL,
 CONSTRAINT [PK_dbo.IdentityUsers] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
)

and the IdentityUserLogin table should look like this: 并且IdentityUserLogin表应如下所示:

CREATE TABLE [dbo].[IdentityUserLogins](
    [LoginProvider] [nvarchar](128) NOT NULL,
    [ProviderKey] [nvarchar](128) NOT NULL,
    [UserId] [nvarchar](128) NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

but when I run my code on a new database I get this: 但是当我在新数据库上运行代码时,我得到了:

CREATE TABLE [dbo].[IdentityUserLogins](
    [UserId] [nvarchar](128) NOT NULL,
    [LoginProvider] [nvarchar](128) NOT NULL,
    [ProviderKey] [nvarchar](128) NOT NULL,
    [IdentityUser_Id] [nvarchar](128) NULL,
 CONSTRAINT [PK_dbo.IdentityUserLogins] PRIMARY KEY CLUSTERED 
(
    [UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

as you can see, the IdentityUser_Id is obsolete, it should be using the primary key. 如您所见, IdentityUser_Id已过时,应使用主键。 That is my objective :) 那是我的目标:)

You might be confusing EF by not providing a Primary key property to your class IdentityUserLogin . 通过不为类IdentityUserLogin提供主键属性,您可能会混淆EF。 I think in this case EF is taking UserId as your primary key. 我认为在这种情况下,EF将UserId作为您的主键。 Since primary key cannot be duplicate, and the relationship you are mapping says WithMany they conflict with each other. 由于主键不能重复,并且您要映射的关系显示WithMany因此它们彼此冲突。

Update 1 更新1

This is conflicting with your model: 这与您的模型冲突:

this.HasRequired(t => t.User).WithRequiredPrincipal();

because on class IdentityUser you have 因为在类IdentityUser您拥有

public virtual ICollection&ltIdentityUserLogin> Logins { get; set; }

You need to use WithMany(t => t.Logins) in order to match your configuration with the model. 您需要使用WithMany(t => t.Logins)来使您的配置与模型匹配。

Update 2 更新2
This is a One-One relationship you need 这是您需要的一对一关系

public class IdentityUser
{
  ... 
  public virtual IdentityUserLogin Login { get; set; }
  ...
}

and

this.HasRequired(t => t.User).WithRequiredPrincipal(t => t.Login);

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

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