tableadapter约束失败

时间:2015-11-05 21:23:39

标签: c# sql exception constraints tableadapter

我在一个大型项目中使用tableadapter并且我不断获得“无法启用约束。一行或多行包含违反非null,唯一或外键约束的值。”每当我打电话给我的任何查询时都会出错。我没有空值,并且在结果集中确实有唯一的主键值。不过,我尝试在生成的源代码中将EnforceConstraints设置为False。我还使用列的上下文菜单中的“删除键”选项删除了所有主键。我为tableadapter中的所有列设置了AllowDBNull为true。例外仍然存在。我在tableadapter下定义了三个查询,所有这些查询都返回相同的完全相同的列(SQL仅在使用的过滤器表达式中有所不同)。在线研究这个问题,我发现在生成的源代码中设置try / catch以检查错误以确定确切原因的建议。当我这样做时,根本不会产生错误。生成的源如下:

public virtual CyberevToo.ReactionImagesDataTable AllMedia(string MindfileID, string FilePath) {
        this.Adapter.SelectCommand = this.CommandCollection[1];
        if ((MindfileID == null)) {
            throw new global::System.ArgumentNullException("MindfileID");
        }
        else {
            this.Adapter.SelectCommand.Parameters[0].Value = ((string)(MindfileID));
        }
        if ((FilePath == null)) {
            throw new global::System.ArgumentNullException("FilePath");
        }
        else {
            this.Adapter.SelectCommand.Parameters[1].Value = ((string)(FilePath));
        }
        CyberevToo.ReactionImagesDataTable dataTable = new CyberevToo.ReactionImagesDataTable();
        this.Adapter.Fill(dataTable);
        return dataTable;
    }

“this.Adapter.Fill(dataTable);”抛出异常线。修改后,源代码如下:

 public virtual CyberevToo.ReactionImagesDataTable AllMedia(string MindfileID, string FilePath) {
        this.Adapter.SelectCommand = this.CommandCollection[1];
        if ((MindfileID == null)) {
            throw new global::System.ArgumentNullException("MindfileID");
        }
        else {
            this.Adapter.SelectCommand.Parameters[0].Value = ((string)(MindfileID));
        }
        if ((FilePath == null)) {
            throw new global::System.ArgumentNullException("FilePath");
        }
        else {
            this.Adapter.SelectCommand.Parameters[1].Value = ((string)(FilePath));
        }
        CyberevToo.ReactionImagesDataTable dataTable = new CyberevToo.ReactionImagesDataTable();
        try
        {
          this.Adapter.Fill(dataTable);
        }
        catch (System.Exception ex) {
          System.Console.WriteLine(ex.Message);
        }
        return dataTable;
    }

在“System.Console.WriteLine(ex.Message);”行上设置断点从来没有达到过。使用try / catch,DataTable可以很好地返回并充满数据。没有它,根本不会返回任何数据。当我使用try / catch构建并运行程序时,不会发生异常。当它在没有try / catch的情况下执行时,它会因上述异常而失败。

任何想法为什么?每次我对其中一个定义进行更改时,我不想为XSD中的所有20个tableadapter重新编码生成的代码!

0 个答案:

没有答案