在MVC 5中使用Microsoft.AspNet.Identity进行脚手架

时间:2015-12-04 01:30:33

标签: c# asp.net-mvc asp.net-identity

我正在尝试使用Scaffolding和Microsoft.AspNet.Identity创建一个新的Controller,但是我收到一个错误说 "IdentityUserLogin" has no key defined.

我尝试在Clients表和Microsoft.AspNet.Identity用户表之间创建关系。

我用来建立关系的字段是UserCreatedUserModifiedUserAssigned

模型是:

[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 个答案:

答案 0 :(得分:1)

可能是因为你定义的是select tareweight0_.CODE as CODE274_0_, tareweight0_.SITE as SITE274_0_, tareweight0_.WEIGHT as WEIGHT274_0_ from TARE_WEIGHT tareweight0_ where tareweight0_.CODE=? and tareweight0_.SITE=? 。我使用以下实现,它很完美 -

select tareweight0_.CODE as CODE274_, tareweight0_.SITE as SITE274_, 
       tareweight0_.WEIGHT as WEIGHT274_ from TARE_WEIGHT tareweight0_ 
       where tareweight0_.CODE=? and tareweight0_.SITE=?

然后像这样使用它 -

select code, site, weight from tare_weight where code like 'LC%';

 CODE   SITE    WEIGHT
 ------ ------ -------
 LC1               .81
 LC2               .83
 LC2    BEENLH     .81
 LC3              1.07
 LC3    BEENLH    1.05
 LC4              1.05
 LCH1              .91
 LCH2              .93
 LCH2   BEENLH     .91
 LCH6             1.13
 LCH6   BEENLH    1.11

编辑:用户管理器需要第二个参数,密钥类型。所以改变它 - 就像这个

select code, site, weight from tare_weight where code like 'LC%' and site IS NULL;

 CODE   SITE    WEIGHT
 ------ ------ -------
 LC1               .81
 LC2               .83
 LC3              1.07
 LC4              1.05
 LCH1              .91
 LCH2              .93
 LCH6             1.13

和其他人 -

ApplicationUser