0

I am using Entity Framework 6.1 version.

I am performing insertion operation on DB.I am planning to insert a list of rows into Db.

Can anyone suggest the best way,as I have to insert bulk records into Db.

Here is my Code:

 public void InsertData(List<TimingModel> modelList)
        {
            foreach (var model in modelList)
            {
                _dbContext.dbSet.Add(model);

            }
            _dbContext.SaveChanges();
        }

Here is my TimingModel

 public class Model
    {
       [DatabaseGenerated(DatabaseGeneratedOption.Computed)] 
        public int UID{get;set;}
        public string Application { get; set; }
        public string Service { get; set; }
        public string Call { get; set; }
        public string Verb { get; set; }
        public string API { get; set; }
        public string HttpResponse { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime StopTime { get; set; }
        public int Duration { get; set; }
        public int SQLDuration { get; set; }
        public string Caller { get; set; }
        public string Nodename { get; set; }
        public string Server { get; set; }
        public string Logfile { get; set; }
        public string Product { get; set; }
        public string Session { get; set; }
        public int OrganisationUID { get; set; }
    }

Below is my Mapper:

 public class TimingMapper: EntityTypeConfiguration<TimingModel>
    {
        public TimingMapper()
        {
            // Primary Key
            this.HasKey(n => n.UID);
           // Table & Column Mappings
            this.ToTable("Timing");

            this.Property(n => n.OrganisationUID).HasColumnName("OrgUID");

            }
    }

I am getting the below Exception:

Modifications to tables where a primary key column has property 'StoreGeneratedPattern' set to 'Computed' are not supported. Use 'Identity' pattern instead. Key column: 'UID'. Table: 'CodeFirstDatabaseSchema.model'.

Can anyone please suggest?

A_Sk
  • 4,532
  • 3
  • 27
  • 51
AMDI
  • 895
  • 2
  • 17
  • 40
  • Try to search bulkcopy. – Anonymous Duck Aug 28 '15 at 10:07
  • it's strange that no exception thrown but data is not inserted? the code looks simple enough to work normally (not regarding of performance issue here). – Hopeless Aug 28 '15 at 10:10
  • @Hopeless- sorry I am able to get exception after adding try catch blocks.I have edited my question with exception detail – AMDI Aug 28 '15 at 10:19
  • Check this link http://stackoverflow.com/questions/12012736/entity-framework-code-first-using-guid-as-identity-with-another-identity-column – Ajinder Singh Aug 28 '15 at 10:20
  • @AjinderSingh- After changing from computed to Identity on Model property,Now I am getting exception: "String or binary data would be truncated. The statement has been terminated." – AMDI Aug 28 '15 at 10:27
  • this may be relevant http://stackoverflow.com/q/23962690/1236044 – jbl Aug 28 '15 at 12:37

1 Answers1

0

Thanks for the support.

I figured out the issue which is in the Database table,the size of the column is less.

It got resolved after increasing the size of column in Database table.

AMDI
  • 895
  • 2
  • 17
  • 40