2015-08-28 23 views
0

我正在使用Entity Framework 6.1版本。面向Db的實體框架插入問題

我正在DB上執行插入操作,我打算將行列表插入到Db中。

任何人都可以提出最好的方法,因爲我必須將批量記錄插入到Db中。

這裏是我的代碼:

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

      } 
      _dbContext.SaveChanges(); 
     } 

這裏是我的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; } 
    } 

下面是我的映射:

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"); 

      } 
    } 

我收到以下異常:

對主鍵列具有屬性的表進行修改 不支持將「StoreGeneratedPattern」設置爲「計算」。改爲使用 '身份'模式。鍵列:'UID'。表: 'CodeFirstDatabaseSchema.model'。

任何人都可以請建議?

+0

嘗試搜索批量拷貝。 – Sherlock

+0

奇怪的是沒有拋出異常但數據沒有被插入?代碼看起來很簡單,可以正常工作(不考慮性能問題)。 – Hopeless

+0

@ Hopeless-對不起,我可以在添加try catch塊後得到異常。我編輯了我的問題,例外詳細信息 –

回答

0

感謝您的支持。

我想出了數據庫表中的問題,列的大小更少。

它在增加數據庫表中列的大小後得到解決。