繁体   English   中英

MySQL Net连接器问题

[英]MySQL Net Connector Issue

我正在使用MySQL Net Connector(http://dev.mysql.com/downloads/connector/net/),并且已将MySQL数据库导入Visual Studio(ADO.NET实体数据模型)并创建了关系。

现在,我想从数据库中收集一些数据到我的自定义业务实体中,并对路由进行如下编码:

public List<Property> GetProperties()
{
    using (ncsEntities dataContext = this.ncsDataContext)
    {

        // Begin Test #1
        var q1 = from p in dataContext.ra_properties.Top("3")
                 where p.street_id > 0
                 select p;

        List<ra_properties> list1 = q1.ToList();
        // End Test #1   list2 is populated as expected
        // The property ra_streets is populated and is not null


        // Begin Test #2
        var q2 = from p in dataContext.ra_properties.Top("3")
                 where p.street_id > 0
                 select new Property
                 {
                     Key2 = p.valuation_id,
                     Address = "Some Dummy Value"
                 };

        List<Property> list2 = q2.ToList();
        // End Test #2
        // list2 is populated as expected.


        // Begin Test #3
        var q3 = from p in dataContext.ra_properties.Top("3")
                 where p.street_id > 0
                 select new Property
                 {
                     Key2 = p.valuation_id,
                     Address = (p.ra_streets == null || p.ra_streets.address_1 == null) ? string.Empty : p.ra_streets.address_1
                 };

        List<Property> list3 = q3.ToList();
        // End Test #3
        // This Test Fails.  The exception message is
        // Object reference not set to an instance of an object.

        return list3;

    }
}

我不知道为什么最后一次测试不起作用。 它失败,并显示异常消息“对象引用未设置为对象的实例”。 有人可以帮我从这里出去吗?

我认为您需要在查询中添加对ra_streets的显式引用。 我目前无法对其进行测试,但似乎确实如此。

尝试将其更改为

// Begin Test #3
var q3 = from p in dataContext.ra_properties.Include("ra_streets").Top("3")
         where p.street_id > 0
         select new Property
         {
             Key2 = p.valuation_id,
             Address = (p.ra_streets == null || p.ra_streets.address_1 == null) ? string.Empty : p.ra_streets.address_1
         };

看看是否可行?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM