SubSonic SimpleRepository 更新导致空引用异常 [英] SubSonic SimpleRepository Updates Cause Null Reference Exceptions

查看:25
本文介绍了SubSonic SimpleRepository 更新导致空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究 SubSonic 的新 SimpleRepository 时,我发现调用 Update() 方法总是抛出 NullReferenceException.这在 3.0.0.3 版本中包含的示例 MVC 下载中也是如此.

In researching the SubSonic's new SimpleRepository, I've found that calling the Update() method always throws a NullReferenceException. This is even true in the sample MVC download that's included with the 3.0.0.3 release.

有谁知道是否有办法让更新成功?

Does anyone know if there's a way to get updates to succeed?

这是一个例子.if 语句有效;它添加表并创建记录.再次运行此代码会流向 else 块,并且更新会引发异常.

Here's an example. The if statement works; it adds the table and creates the record. Running this code a second time flow to the else block, and the update throws the exception.

var repo = new SimpleRepository("c", SimpleRepositoryOptions.RunMigrations);

var user = repo.Single<User>(u => u.Email == "a@b.com");

if (user == null)
{
    repo.Add(new User { Email = "a@b.com", Name = "Test" });
}
else
{
    user.Name = DateTime.Now.ToString();
    repo.Update(user);
}

public class User
{
    public int Key { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

推荐答案

我想我发现了问题.在 SubSonic 源代码中,Update 例程存在一个小缺陷,它在更新查询对象中查询表列表以获取列名.Linq 查询需要使用列的 QualifiedName 属性,而不是 Name 属性.查询设置(即查询的右侧)使用完全限定名称.

I think I found the problem. In the SubSonic source there's a minor flaw in the Update routine where it queries the list of tables in the update query object for a column name. The Linq query needed to use the column's QualifiedName property, not the Name property. The query settings (which is the right hand side of the query) uses the fully qualified name.

我也冒昧地在 SubSonic 的 GitHub 站点上提交了一个问题 :)

I took the liberty of submitting an issue on SubSonic's GitHub site as well :)

对于那些感兴趣的人,问题在 Update.cs(在 Query 文件夹中),第 229 行.

For those interested, the issue is in Update.cs (in the Query folder), Line 229.

改变这个...

var col= table.Columns.SingleOrDefault(
  x => x.Name.Equals(s.ColumnName, StringComparison.InvariantCultureIgnoreCase)
);

这...

var col = table.Columns.SingleOrDefault(
  x => x.QualifiedName.Equals(
    s.ColumnName, StringComparison.InvariantCultureIgnoreCase
  )
);

重建,一切顺利.

这篇关于SubSonic SimpleRepository 更新导致空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆