【问题标题】:Facing issue with entity framework insert to Db面临实体框架插入 Db 的问题
【发布时间】:2015-08-28 10:01:51
【问题描述】:

我使用的是 Entity Framework 6.1 版本。

我正在对 DB 执行插入操作。我打算将行列表插入到 Db 中。

任何人都可以建议最好的方法,因为我必须将批量记录插入 Db。

这是我的代码:

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

            }
            _dbContext.SaveChanges();
        }

这是我的计时模型

 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”设置为“Computed”。采用 代替“身份”模式。键列:'UID'。桌子: 'CodeFirstDatabaseSchema.model'。

谁能推荐一下?

【问题讨论】:

  • 尝试搜索bulkcopy。
  • 奇怪的是没有抛出异常却没有插入数据?代码看起来很简单,可以正常工作(这里不考虑性能问题)。
  • @Hopeless- 抱歉,在添加 try catch 块后我能够得到异常。我已经用异常详细信息编辑了我的问题
  • @AjinderSingh- 在模型属性上从计算更改为身份后,现在我收到异常:“字符串或二进制数据将被截断。语句已终止。”

标签: c# sql-server entity-framework entity-framework-6


【解决方案1】:

感谢您的支持。

我发现问题出在数据库表中,列的大小变小了。

在增加数据库表中的列大小后得到解决。

【讨论】:

    猜你喜欢
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多