2015-12-04 2 views
1

Я пытаюсь создать новый контроллер с помощью Строительные леса и Microsoft.AspNet.Identity, но я получаю сообщение об ошибке, что "IdentityUserLogin" has no key defined.Строительные леса с Microsoft.AspNet.Identity в MVC 5

Я пытаюсь создать отношения между таблицей Клиентов и таблицей пользователя Microsoft.AspNet.Identity.

Поля, которые я использую для построения отношений, являются UserCreated, UserModified и UserAssigned.

Модель:

[Table("Clients")] 
public partial class Client 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    [Display(Name = "Criado em")] 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-M-yyyy HH:mm}")] 
    public DateTime DateCreated { get; set; } 

    [Display(Name = "Alterado em")] 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-M-yyyy HH:mm}")] 
    public DateTime DateModified { get; set; } 

    [MaxLength(128)] 
    public virtual string UserCreated_Id { get; set; } 

    [ForeignKey("UserCreated_Id")] 
    public virtual ApplicationUser UserCreated { get; set; } 

    [MaxLength(128)] 
    public virtual string UserModified_Id { get; set; } 

    [ForeignKey("UserModified_Id")] 
    public virtual ApplicationUser UserModified { get; set; } 

    [MaxLength(128)] 
    public virtual string UserAssigned_Id { get; set; } 

    [ForeignKey("UserAssigned_Id")] 
    public virtual ApplicationUser UserAssigned { get; set; } 

    [Required] 
    [MaxLength(256)] 
    [Display(Name = "Nome")] 
    public string Name { get; set; } 

    [Display(Name = "Morada")] 
    [MaxLength(512)] 
    public string Address { get; set; } 

    public int PostalCode { get; set; } 
    public int PostalCodeExtension { get; set; } 

    [MaxLength(256)] 
    public string PostalCodeCity { get; set; } 

    [Display(Name = "Telefone")] 
    [MaxLength(50)] 
    public string Telephone { get; set; } 

    [Display(Name = "Fax")] 
    [MaxLength(50)] 
    public string Fax { get; set; } 

    [Display(Name = "Email")] 
    [MaxLength(256)] 
    public string Email { get; set; } 

    [Display(Name = "Contribuinte")] 
    [MaxLength(50)] 
    public string VAT { get; set; } 
} 

ответ

1

Может быть, потому что это то, как вы определили ApplicationUser. Я использую следующую реализацию, и она работает идеально -

public class ApplicationUserLogin : IdentityUserLogin<int> 
{ 
} 

public class ApplicationUserRole : IdentityUserRole<int> 
{ 

} 

public class ApplicationUserClaim : IdentityUserClaim<int> 
{ 
} 

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
{ 

} 

Затем использовать его как это -

public class DomainEntity : ..... 
{ 
    [Key] 
    public virtual int Id { get; set; } 
    public virtual ApplicationUser CreatedBy { get; set; } 

    public virtual ApplicationUser LastUpdatedBy { get; set; } 

} 

EDIT: менеджер пользователя требуется второй параметр, тип ключа. Так как изменить его - это

UserManager<ApplicationUser, int> 

и другие -

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
+0

Привет, спасибо, я использую шаблон MVC 5 и у меня нет доступа к определению Userlogin – Patrick

+1

Тогда просто игнорировать его, добавить только требуемые. 'IdentityUser' имеет несколько реализаций. –

+0

Я получаю строку: public async Задача GenerateUserIdentityAsync (UserManager manager), подчеркнутая в разделе «manager» – Patrick

Смежные вопросы