简体   繁体   中英

Facing issue with entity framework insert to Db

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM