流畅的 nhibernate 可空外键“空引用异常”

发布于 2024-12-17 13:32:21 字数 1277 浏览 0 评论 0原文

我刚刚开始使用 nHibernate Fluent,并且我的关联一切进展顺利,直到我遇到一个 StateID(这是一个外键,见下文)为空的位置。 得到一个错误 “空引用异常”

然后我在下面的这行 Console.WriteLine(location.address + ", " + location.State.state);

public static void LoadLocationsFromDatabase()
{
    using (var session = NHibernateHelper.OpenSession())
    {
        // retreive all stores and display them
        using (session.BeginTransaction())
        {
            var locations = session.CreateCriteria(typeof(Location))
                .List<Location>();

            var i = 0;
            foreach (var location in locations)
            {
                i++;
                Console.WriteLine(location.address + ", " + location.State.state);
            }
        }

    }
}

public LocationMap()
{
    Id(x => x.locationID).Column("ID");
    Map(x => x.address).Column("Address");
    References(x => x.State)
  .Column("StateID")
      .Cascade.All();
}

public class State
{
    public virtual int ID { get; set; }
    public virtual String state { get; set; }
    public virtual IList<Location> Locations { get; set; }
}

我猜该值在数据库中可能不应该为空。 StateID是states表上ID的外键,所以我想它不应该允许为空?

无论如何,我觉得我的代码应该能够以某种方式允许这种情况,那么我该如何解决这个问题呢?

I have just started with nHibernate Fluent and all is going well with my associations, until I hit a location that have a StateID (which is a foreign key, see below) of null. Then I get an error "null reference exception"

on this line Console.WriteLine(location.address + ", " + location.State.state);

below:

public static void LoadLocationsFromDatabase()
{
    using (var session = NHibernateHelper.OpenSession())
    {
        // retreive all stores and display them
        using (session.BeginTransaction())
        {
            var locations = session.CreateCriteria(typeof(Location))
                .List<Location>();

            var i = 0;
            foreach (var location in locations)
            {
                i++;
                Console.WriteLine(location.address + ", " + location.State.state);
            }
        }

    }
}

public LocationMap()
{
    Id(x => x.locationID).Column("ID");
    Map(x => x.address).Column("Address");
    References(x => x.State)
  .Column("StateID")
      .Cascade.All();
}

public class State
{
    public virtual int ID { get; set; }
    public virtual String state { get; set; }
    public virtual IList<Location> Locations { get; set; }
}

I guess this value might shouldn't probably be null in the DB. StateID is a foreign key to ID on the states table, so I guess it shouldn't be allowed to be null?

Anyway I feel that my code should be able to allow for this one way or another, so how can I get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2024-12-24 13:32:21
Console.WriteLine(location.address + ", " + (location.State == null ? "Unknown" : location.State.state));
Console.WriteLine(location.address + ", " + (location.State == null ? "Unknown" : location.State.state));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文