2011-03-29 88 views
0

我正在使用MySQL网络连接器(http://dev.mysql.com/downloads/connector/net/),并且已将MySQL数据库导入Visual Studio(ADO.NET实体数据模型)并建立了关系。MySQL网络连接器问题

我现在想从数据库中的一些数据收集到我的自定义业务实体和已经编写路由如下:

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; 

    } 
} 

我想不通为什么上次测试不起作用。它失败,异常消息对象引用未设置为对象的实例。 任何人都可以帮助我吗?

+0

一个接一个的测试..确保你的连接字符串也是正确的...如果你真的连接。 – Crimsonland 2011-03-29 03:07:38

+0

你可以发布堆栈跟踪吗? – 2011-03-29 03:39:08

+0

堆栈跟踪System.Data.Entity.dll!System.Data.Objects.ObjectQuery .GetResults(System.Data.Objects.MergeOption?forMergeOption)+ 0x96字节 \t System.Data.Entity.dll!系统.Data.Objects.ObjectQuery .System.Collections.Generic.IEnumerable .GetEnumerator()+ 0x2d字节 \t mscorlib.dll中!System.Collections.Generic.List 的.List(系统。 Collections.Generic.IEnumerable 集合)+ 0x13c字节 – Allan 2011-03-30 00:52:03

回答

0

我认为您需要在您的查询中明确提及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 
     }; 

,看看它是否起作用?

+0

我用Include子句测试了上面的内容,但是没有改变结果。错误消息仍然相同。 – Allan 2011-03-30 00:46:07