Invalid Column name ID-Entity Framework

c# entity-framework-6

Question

I have table in my database with this structure:

CREATE TABLE [dbo].[FDTransaction]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [FamilyId] [int] NULL,
    [BankId] [int] NULL,
    [Account_No] [numeric](25, 0) NULL,
    [Amount] [int] NULL,
    [Interest_Percent] [decimal](18, 0) NULL,
    [Tenure] [int] NULL,
    [MaturityDate] [datetime] NULL,
    [InterestAmount] [int] NULL,

    CONSTRAINT [PK_FDTransaction] 
        PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]

I have a model class which represents the database table in the code:

 public class FDTransaction
 {
        [Key]
        public int ID { get; set; }     
        public int Familyid { get; set; }
        public int BankId { get; set; }
        public decimal Account_No { get; set; }
        public int Amount { get; set; }
        public double Interest_Percent { get; set; }
        public int Tenure { get; set; }
        public DateTime MaturityDate { get; set; }
        public int interestAmount { get; set; }
}

I am getting an error when I am adding a record in table at below mentioned line.

dbentities.Transactions.Add(transaction);

Also below is the error stack.

       StackTrace:
            at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
            at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
            at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
            at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
            at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
            at System.Data.Entity.Internal.InternalContext.SaveChanges()
       InnerException: System.Data.SqlClient.SqlException
            HResult=-2146232060
            Message=Invalid column name 'ID'.
Invalid column name 'ID'.
            Source=.Net SqlClient Data Provider
            ErrorCode=-2146232060
            Class=16
            LineNumber=5
            Number=207

As mentioned above, I do have column ID in table. Also I have property ID in class FDTransaction. Still I am not able to add row in the database.

1
2
4/1/2018 12:49:50 PM

Popular Answer

Since the ID column is an IDENTITY column, you need to add another attribute ([DatabaseGenerated]) to your ID field in the model class to tell EF that this is an IDENTITY column and it doesn't need to provide a value - SQL Server will set the value when you insert the row.

Try this:

public class FDTransaction
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }     
    .... (your other properties here, same as before) ......  
}
0
4/1/2018 1:40:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow