Note that there are some explanatory texts on larger screens.

plurals
  1. POEF Unidirectional One to Many relationship
    primarykey
    data
    text
    <p>I have an <strong>IdentityUser</strong> class which has a navigation property called <strong>Logins</strong> which is of type <strong>Collection</strong>.</p> <pre><code>public class IdentityUser : IUser { public IdentityUser(); public IdentityUser(string userName); public virtual ICollection&lt;IdentityUserClaim&gt; Claims { get; } public virtual string Id { get; set; } public virtual ICollection&lt;IdentityUserLogin&gt; Logins { get; } public virtual string PasswordHash { get; set; } public virtual ICollection&lt;IdentityUserRole&gt; Roles { get; } public virtual string SecurityStamp { get; set; } public virtual string UserName { get; set; } } </code></pre> <p>In the <strong>IdentityUserLogin</strong> class there is a navigation property called <strong>User</strong> which is of type <strong>IdentityUser</strong>.</p> <pre><code>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; } } </code></pre> <p>From this I can see that </p> <ol> <li>the <strong>IdentityUser</strong> can have 0 to many <strong>IdentityUserLogin</strong>.</li> <li><strong>IdentityUserLogin</strong> must have a <strong>IdentityUser</strong>.</li> </ol> <p>I tried:</p> <pre><code> this.HasRequired(t =&gt; t.User) .WithRequiredPrincipal(); </code></pre> <p>but it was looking for a foreign key of <strong>IdentityUser_Id</strong> but it should be using <strong>UserId</strong> (which is a property of <strong>IdentityUserLogin</strong></p> <p>so then I tried:</p> <pre><code> this.HasRequired(t =&gt; t.User) .WithMany(t =&gt; t.Logins) .HasForeignKey(t =&gt; t.UserId); </code></pre> <p>but I get an error stating:</p> <blockquote> <p>IdentityUserLogin_User_Source: : Multiplicity is not valid in Role 'IdentityUserLogin_User_Source' in relationship 'IdentityUserLogin_User'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.</p> </blockquote> <p>Does anyone know how I can declare the relationship and specify the Foreign Key?</p> <p><strong>Update 1</strong></p> <p>This is my <strong>IdentityUserLogin</strong> map class:</p> <pre><code>public class IdentityUserLoginMap : EntityTypeConfiguration&lt;IdentityUserLogin&gt; { public IdentityUserLoginMap() { // Primary Key this.HasKey(t =&gt; t.UserId); // Properties this.Property(t =&gt; t.LoginProvider) .IsRequired() .HasMaxLength(128); this.Property(t =&gt; t.ProviderKey) .IsRequired() .HasMaxLength(128); this.Property(t =&gt; t.UserId) .IsRequired() .HasMaxLength(128); // Table &amp; Column Mappings this.ToTable("IdentityUserLogins"); this.Property(t =&gt; t.LoginProvider).HasColumnName("LoginProvider"); this.Property(t =&gt; t.ProviderKey).HasColumnName("ProviderKey"); this.Property(t =&gt; t.UserId).HasColumnName("UserId"); // Relationships this.HasRequired(t =&gt; t.User) .WithRequiredPrincipal(); } } </code></pre> <p>As you can see, I am setting a primary key</p> <p><strong>Update 2</strong></p> <p>Just to give a bit more of an explanation, here is the users table:</p> <pre><code>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] ) </code></pre> <p>and the <strong>IdentityUserLogin</strong> table should look like this:</p> <pre><code>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] </code></pre> <p>but when I run my code on a new database I get this:</p> <pre><code>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] </code></pre> <p>as you can see, the <strong>IdentityUser_Id</strong> is obsolete, it should be using the primary key. That is my objective :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload