代码之家  ›  专栏  ›  技术社区  ›  Gideok Seong

如何在实体框架中启用IS标识?

  •  1
  • Gideok Seong  · 技术社区  · 5 年前

    enter image description here

    我试图在代码中使用is identity来增加transactionid。但它显示以下错误:

    SqlException:无法在中插入标识列的显式值 当identity_insert设置为off时的表“transactionhistories”。

    我试着把 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 句法。但没用。

    public partial class TransactionHistories
        {
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int TransactionId { get; set; }
    
            public DateTime? TransactionDate { get; set; }
            public long? CheckingAccountNumber { get; set; }
            public long? SavingAccountNumber { get; set; }
            [DisplayName("Transffered Amount")]
            public decimal? Amount { get; set; }
            public decimal? Transactionfee { get; set; }
            public long? TransactionTypeId { get; set; }
        }
    

                TransactionHistories = new TransactionHistories();
                TransactionTypes = new TransactionTypes();
    
                if (!ModelState.IsValid)
                {
                    return Page();
                }
    
                SavingAccounts.Balance = SavingAccounts.Balance - Amount;
                CheckingAccounts.Balance = CheckingAccounts.Balance + Amount;
    
                //TransactionHistories.TransactionId = TransactionHistories.TransactionId + 1;
                TransactionHistories.TransactionDate = DateTime.Now;
                TransactionHistories.CheckingAccountNumber = CheckingAccounts.CheckingAccountNumber;
                TransactionHistories.SavingAccountNumber = SavingAccounts.SavingAccountNumber;
                TransactionHistories.Amount = Amount;
                TransactionHistories.Transactionfee = 0;
                TransactionHistories.TransactionType = "SavingToChecking";
    

    上面是我的模型类,transactionid是主键,这是我试图使用is identity使其增加1的值。我怎样才能做到这一点呢?请给我任何建议和帮助。

    1 回复  |  直到 5 年前