Установка первичного ключа IdentityUserRole. Для типа сущности IdentityUserRole<Guid> требуется определить первичный клюC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Установка первичного ключа IdentityUserRole. Для типа сущности IdentityUserRole<Guid> требуется определить первичный клю

Сообщение Anonymous »


I'm trying to extend the identity entities however am experiencing an issue with my entity which extends from IdentityUserRole, giving a runtime error of:

Unable to create a 'DbContext' of type ''. The exception 'The entity type 'IdentityUserRole' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I've extended all identity tables:

public class AppRole : IdentityRole { } public class AppRoleClaim : IdentityRoleClaim { } public class AppUser : IdentityUser { } public class AppUserClaim : IdentityUserClaim { } public class AppUserLogin : IdentityUserLogin { } public class AppUserRole : IdentityUserRole { } public class AppUserToken : IdentityUserToken { } And my context is defined as:

public class AppDBContext(DbContextOptions options) : AppDBContext(options) { } The primary key for the AppUserRole table entity should consist of the two foreign keys pointing to the related tables, UserId and RoleId. This is the default primary key, however does not appear to be working when extending the IdentityUserRole table.

I've tried the suggestions in the docs:

Annotations dont work

I have tried to specify this primary key using attributes, however this makes no difference (It seems like none of the common attributes are acknowledged on the identity tables, TableAttribute, PrimaryKeyAttribute etc).

[PrimaryKey(nameof(UserId), nameof(RoleId))] public class AppUserRole : IdentityUserRole { } Fluent API doesnt work

I've also tried setting this via the fluent API in the OnModelCreating method, however again this doesnt seem to make any difference and I get the same error:

protected override void OnModelCreating(ModelBuilder builder) { this.OnModelCreating(builder); builder.Entity().HasKey(e => new { e.UserId, e.RoleId }); } How can I specify the primary key for this table?

Interestingly, if I define my context with the default IdentityUserRole type, it works absolutely fine:

public class AppDBContext(DbContextOptions options, IConfiguration config) : AppDBContext(options) { } For some reason, EF just isnt able to work out the primary key when extending the IdentityUserRole entity, and it seems to be ignoring my attempts to specify the primary key.

Any help resolving this would be appreciated.


Источник: https://stackoverflow.com/questions/780 ... erroleguid
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»