我如何将自定义制作的虚拟对象关联到像实体框架一样工作的关系对象?
我有一个customerdetails
和customerAddress
对象。构造函数如下:
public class CustomerDetails
{
[Key]
public int CustomerID { get; set; }
[Required]
[StringLength(50)]
public string CustomerName { get; set; }
public int AddressID { get; set; }
public virtual CustomerAddress customerAddress { get; set; }
}
public class CustomerAddress
{
[Key]
public int AddressID { get; set; }
[Required]
[StringLength(200)]
public string Address { get; set; }
public virtual ICollection<CustomerDetails> CustomerDetails { get; set; }
}
我已经从生成的实体框架中复制了这两个构造函数。
填充客户列表和地址列表
List<CustomerDetails> customers = new List<CustomerDetails>();
List<CustomerAddress> addresses = new List<CustomerAddress>();
SqlCommand cmd = new SqlCommand("select * from Customerdetails", con);
using (IDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
CustomerDetails customer = new CustomerDetails
{
CustomerID = Convert.ToInt32(rdr["CustomerID"]),
AddressID = Convert.ToInt32(rdr["AddressID"]),
CustomerName = rdr["CustomerName"].ToString()
};
customers.Add(customer);
}
}
cmd = new SqlCommand("select * from CustomerAddress", con);
using (IDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
CustomerAddress address = new CustomerAddress
{
AddressID = Convert.ToInt32(rdr["AddressID"]),
Address = rdr["Address"].ToString()
};
addresses.Add(address);
}
}
我已经使用sqldatareader
在尝试使用linq绑定数据以显示客户地址时
GridView1.DataSource = from c in customers
select new { c.CustomerName, c.customerAddress.AddressID};
GridView1.DataBind();
,它不按预期工作。我有一个错误:
对象引用未设置为对象的实例
对象引用未设置为我知道会发生的 。但是,使虚拟对象链接到关系对象以使参考可以工作的诀窍是什么?
I have a CustomerDetails
and a CustomerAddress
object. The constructors are as follow:
public class CustomerDetails
{
[Key]
public int CustomerID { get; set; }
[Required]
[StringLength(50)]
public string CustomerName { get; set; }
public int AddressID { get; set; }
public virtual CustomerAddress customerAddress { get; set; }
}
public class CustomerAddress
{
[Key]
public int AddressID { get; set; }
[Required]
[StringLength(200)]
public string Address { get; set; }
public virtual ICollection<CustomerDetails> CustomerDetails { get; set; }
}
I have copied these two constructors from generated code of Entity Framework.
I have used SqlDataReader
to populate the customer list and address list
List<CustomerDetails> customers = new List<CustomerDetails>();
List<CustomerAddress> addresses = new List<CustomerAddress>();
SqlCommand cmd = new SqlCommand("select * from Customerdetails", con);
using (IDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
CustomerDetails customer = new CustomerDetails
{
CustomerID = Convert.ToInt32(rdr["CustomerID"]),
AddressID = Convert.ToInt32(rdr["AddressID"]),
CustomerName = rdr["CustomerName"].ToString()
};
customers.Add(customer);
}
}
cmd = new SqlCommand("select * from CustomerAddress", con);
using (IDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
CustomerAddress address = new CustomerAddress
{
AddressID = Convert.ToInt32(rdr["AddressID"]),
Address = rdr["Address"].ToString()
};
addresses.Add(address);
}
}
When I try to bind data using LINQ to display customer's address, it doesn't work as intended.
GridView1.DataSource = from c in customers
select new { c.CustomerName, c.customerAddress.AddressID};
GridView1.DataBind();
I get an error:
Object reference not set to an instance of an object
I knew it's going to happen. But what's the trick to make the virtual objects linking to the relational object so that the reference will work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可能需要自己填充虚拟属性,或者只是删除它们并在查询中使用内部连接。
I think you might need to populate the virtual properties yourself, or just remove them and use an inner join in your query.