我如何将自定义制作的虚拟对象关联到像实体框架一样工作的关系对象?

发布于 2025-01-31 03:52:37 字数 2139 浏览 3 评论 0原文

我有一个customerdetailscustomerAddress对象。构造函数如下:

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 技术交流群。

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

发布评论

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

评论(1

夏の忆 2025-02-07 03:52:37

我认为您可能需要自己填充虚拟属性,或者只是删除它们并在查询中使用内部连接。

// populate
For Each CustomerDetail from db        
    IF CustomerDetail.AddressId NOT IN addresses
       Fetch CustomerAddress from Db
       addresses.Add(CustomerAddress )
    ELSE
       FETCH CustomerAddress from addresses
    END
    CustomerDetail.customerAddress = CustomerAddress
    CustomerAddress.CustomerDetails.Add(CustomerDetail)
    customers.Add(CustomerDetail)


//inner join
from c in customers
      join a in addresses on c.AddressID equals a.AddressID
      select new { c.CustomerName, a.Address}

I think you might need to populate the virtual properties yourself, or just remove them and use an inner join in your query.

// populate
For Each CustomerDetail from db        
    IF CustomerDetail.AddressId NOT IN addresses
       Fetch CustomerAddress from Db
       addresses.Add(CustomerAddress )
    ELSE
       FETCH CustomerAddress from addresses
    END
    CustomerDetail.customerAddress = CustomerAddress
    CustomerAddress.CustomerDetails.Add(CustomerDetail)
    customers.Add(CustomerDetail)


//inner join
from c in customers
      join a in addresses on c.AddressID equals a.AddressID
      select new { c.CustomerName, a.Address}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文