保存更改时出错EF6“保存或接受更改”

时间:2018-01-24 17:44:41

标签: c# sql-server entity-framework

保存或接受更改失败,因为“Models.AccountSecurityToken”类型的多个实体具有相同的主键值。确保显式设置的主键值是唯一的。确保在数据库和Entity Framework模型中正确配置了数据库生成的主键。使用实体设计器进行数据库优先/模型优先配置。使用'HasDatabaseGeneratedOption'流畅API或'DatabaseGeneratedAttribute'进行代码优先配置。

的DbContext

public class Entities : DbContext
{
    public Entities() : base("name=Entities")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Entities>());
        Database.SetInitializer(new CreateDatabaseIfNotExists<Entities>());
    }
    public virtual DbSet<Admin> Admins { get; set; }
    public virtual DbSet<Account> Accounts { get; set; }
    public virtual DbSet<Product> Products { get; set; }
    public virtual DbSet<Category> Categories { get; set; }
    public virtual DbSet<Contacts> Contacts { get; set; }
    public virtual DbSet<AdminSecurityToken> AdminSecurityTokens { get; set; }
    public virtual DbSet<AccountSecurityToken> AccountSecurityTokens { get; set; }
    public virtual DbSet<SecurityToken> SecurityTokens { get; set; }
    public virtual DbSet<SystemUser> SystemUsers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>().Map(m => m.MapInheritedProperties());
        modelBuilder.Entity<Category>().Map(m => m.MapInheritedProperties());
        modelBuilder.Entity<SystemUser>().Map(m => m.MapInheritedProperties());
        modelBuilder.Entity<Contacts>().Map(m => m.MapInheritedProperties());
        modelBuilder.Entity<SecurityToken>().Map(m => m.MapInheritedProperties());

        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
}

安全令牌模型

public abstract partial class BaseModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public DateTime CreatedDate { get; set; }
    public DateTime? UpdatedDate { get; set; }

    [ForeignKey("CreatedBy")]
    public int? CreatedById { get; set; }
    [ForeignKey("UpdatedBy")]
    public int? UpdatedById { get; set; }

    public bool IsDeleted { get; set; }

    public SystemUser CreatedBy { get; set; }
    public SystemUser UpdatedBy { get; set; }
}
[Table("SecurityToken", Schema = "Security")]
public abstract partial class SecurityToken : BaseModel
{
    public string Token { get; set; }
    public DateTime ExpirationDate { get; set; }
}
[Table("AccountSecurityToken", Schema = "Security")]
public partial class AccountSecurityToken : SecurityToken
{
    [ForeignKey("Account")]
    public int AccountId { get; set; }
    public Account Account { get; set; }

}
[Table("AdminSecurityToken", Schema = "Security")]
public partial class AdminSecurityToken : SecurityToken
{
    [ForeignKey("Admin")]
    public int AdminId { get; set; }
    public Admin Admin { get; set; }
}

我在AccountSecurityToken表中插入新对象时遇到此异常 数据已成功切换到表但未检索

更新

我在同一个项目中与另一个类具有相同的层次结构,并且工作正常。

[Table("SystemUser", Schema = "SystemUser")]
public abstract class SystemUser : BaseModel
{
    [Index(IsUnique = true)]
    [StringLength(100)]
    public string Email { get; set; }
    public string Password { get; set; }
    public string ProfileImageUrl { get; set; }

    [ForeignKey("UpdatedById")]
    public List<BaseModel> UpdatedEntities { get; set; }
    [ForeignKey("CreatedById")]
    public List<BaseModel> CreatedEntities { get; set; }
}

 [Table("Account", Schema = "SystemUser")]
public partial class Account : SystemUser
{
    [StringLength(15)]
    public string FName { get; set; }
    [StringLength(15)]
    public string MName { get; set; }
    [StringLength(15)]
    public string LName { get; set; }

    [ForeignKey("AccountId")]
    public virtual ICollection<Contacts> Contacts { get; set; }

    [ForeignKey("AccountId")]
    public virtual ICollection<AccountSecurityToken> Tokens { get; set; }

}
  [Table("Admin", Schema = "SystemUser")]
public partial class Admin : SystemUser
{
    public AdminType AdminType { get; set; }

    [ForeignKey("AdminId")]
    public virtual ICollection<AdminSecurityToken> Tokens { get; set; }
}

0 个答案:

没有答案