简体   繁体   English

MySQL Net连接器问题

[英]MySQL Net Connector Issue

I am using the MySQL Net Connector (http://dev.mysql.com/downloads/connector/net/) and I have imported the MySQL database into Visual Studio (ADO.NET Entity Data Model) and created the relations. 我正在使用MySQL Net Connector(http://dev.mysql.com/downloads/connector/net/),并且已将MySQL数据库导入Visual Studio(ADO.NET实体数据模型)并创建了关系。

I now want to Collect some data from the database into my custom Business Entity and have coded the routing as follows: 现在,我想从数据库中收集一些数据到我的自定义业务实体中,并对路由进行如下编码:

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;

    }
}

I can not figure out why the last Test does not work. 我不知道为什么最后一次测试不起作用。 It fails with the exception message Object reference not set to an instance of an object. 它失败,并显示异常消息“对象引用未设置为对象的实例”。 Can anyone help me out here? 有人可以帮我从这里出去吗?

I think you need to add explicit reference to ra_streets in your query. 我认为您需要在查询中添加对ra_streets的显式引用。 I cannot test it at the moment but it seems like it is the case. 我目前无法对其进行测试,但似乎确实如此。

Try to change it to 尝试将其更改为

// 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
         };

and see if it works? 看看是否可行?

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

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