0

我首先将实体框架代码与 dotConnect for MySQL 一起使用。

context.SaveChanges()在将对象添加到上下文后调用时出现以下错误:

DbUpdateException
更新条目时发生错误。有关详细信息,请参阅内部异常。

内部异常:
UpdateException:更新条目时出错。有关详细信息,请参阅内部异常。

内部异常:EntityCommandCompilationException:准备命令定义时出错。有关详细信息,请参阅内部异常。"}

内部异常:
NotSupportedException:{“空插入语句。”}

DbContext在我将项目添加到后保存时DbContext

奇怪的是,当我添加它的所有内容时,它添加成功,但是当我添加对象本身时,我得到了这个错误。

这行代码失败:

context.TournamentTables.Add(tournamentTable);

虽然这个工作完美:

tournamentTable.Columns.ForEach(column =>
                                   {
                                       column.Cells.ForEach((cell) => context.TournamentCells.Add(cell));
                                       context.TournamentColumns.Add(column);

                                   });

这是我尝试添加的对象:

public class TournamentTable //: IEnumerable<TournamentColumn>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int? Id { get; set; }

        public List<TournamentColumn> Columns { get; set; }

        public TournamentTable()
        {
            Columns = new List<TournamentColumn>();
        }

        public void AddColumn(int index, TournamentColumn column)
        {
            Columns.Insert(index, column);
        }

        public void RemoveColumn(int index)
        {
            Columns.RemoveAt(index);
        }

        /// <summary>
        /// Add a tournament cell at the top of the column.
        /// </summary>
        /// <param name="column"></param>
        public void AddColumn(TournamentColumn column)
        {
            Columns.Add(column);
        }

        /// <summary>
        /// Returns the tournament column at the index specified.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        [NotMapped]
        public TournamentColumn this[int index]
        {
            get { return Columns[index]; }
        }

        /// <summary>
        /// Returns the tournament cell at the index specified.
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="cellIndex"></param>
        /// <returns></returns>
        [NotMapped]
        public TournamentCell this[int columnIndex, int cellIndex]
        {
            get { return Columns[columnIndex][cellIndex]; }
            set
            {

                Columns[columnIndex][cellIndex] = value;
            }
        }
    }

请注意,Tournament Table Id 默认为 0,如果 Id 声明为“int?” 比它将为空。我已经尝试了这两种情况,也自己设置了 id,但仍然失败。

4

1 回答 1

0

我认为您以错误的方式添加列。

基本上,你不需要这个:

tournamentTable.Columns.ForEach(column =>
{
    column.Cells.ForEach((cell) => context.TournamentCells.Add(cell));
    context.TournamentColumns.Add(column);
});

实体框架会为您做到这一点。您只需context.TournamentTables.Add(tournamentTable)在将所有列添加到锦标赛表后调用。

于 2012-09-29T05:49:06.920 回答