无法使用实体框架从表中读取数据

时间:2018-12-12 07:23:03

标签: entity-framework-6.2

我正在使用实体框架6.2代码优先方法。 我已经创建了这三个类

第一堂课是:

[Table("UserAccount")]
public class UserAccount
{
    [Key()]
    [ForeignKey("Employee")]
    public Guid UserAccountId { get; set; }

    [MaxLength(255)]
    public string UserName { get; set; }

    [MaxLength(255)]
    public string Password { get; set; }

    private  UserPermissions _UserPermissions = new UserPermissions();
    [Required]
    public UserPermissions UserPermissions { get { return _UserPermissions; } set { _UserPermissions = value; } }

    public virtual Employee Employee { get; set; }
}

第二堂课

[Table("UserPermissions")]
public class UserPermissions
{

    [Key(),ForeignKey("UserAccount")]
    public Guid UserPermissionsId { get; set; }

    private Permission _ForUserAccount = new Permission();
    public Permission ForUserAccount
    { get { return _ForUserAccount; } set { _ForUserAccount = value; } }

    private Permission _ForGainReport = new Permission();
    public Permission ForGainReport { get { return _ForGainReport; } set { _ForGainReport = value; } }

    private Permission _ForBills = new Permission();
    public Permission ForBills { get { return _ForBills; } set { _ForBills = value; } }


    private Permission _ForEmployees = new Permission();
    public Permission ForEmployees { get { return _ForEmployees; } set { _ForEmployees = value; } }

    public virtual UserAccount UserAccount { get; set; }

}

第三类是复杂类型

[ComplexType]
public class Permission
{      
    public bool CanAdd { get; set; }

    public bool CanModify { get; set; }

    public bool CanDelete { get; set; }

    public bool CanPreview { get; set; }

    public bool CanPrint { get; set; }

    public bool CanImport { get; set; }

    public bool CanExport { get; set; }
}

当我为数据库添加种子时,一切都很好,并且UserPermissionsTable已适当填充 但是当应用程序正在运行时,该表没有读取任何数据,并且当我尝试从GUI保存更改时,出现以下异常

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll

Additional information: Violation of PRIMARY KEY constraint 'PK_dbo.UserPermissions'. Cannot insert duplicate key in object 'dbo.UserPermissions'. The duplicate key value is (b3b9e826-defd-e811-8d24-6c71d92c1f7e).

我不知道到底是怎么回事。

我检查了一下,一切对我来说都很好。 请帮忙!!!!!!!!!!!!!!!

0 个答案:

没有答案