Linq to SQL:如何更新包含多个表数据的表单视图?

时间:2010-02-04 08:34:03

标签: c# asp.net linq-to-sql formview multiple-tables

举一个简单的例子:

我有一个customer表和一个包含customerID字段的帐户表。我想要formview,它可以更新两个表中的数据。

的FormView:

First Name (Customer)
Last Name (Customer)
Address (Customer)
AccountRate (Account)

如何使用Linq to SQL执行此操作?我只使用像Bind这样的Bind语句(“Account.AccountRate”)吗?或者我是否必须在ItemUpdating事件或类似事件中做一些魔术?

3 个答案:

答案 0 :(得分:1)

你需要加入

from c in db.Customer
join a in db.Account
on a.customerID equals c.customerID
select new Model {FirstName = c.firstName,LastName = c.lastName,Address = c.address,AccountRate = a.accountRate}

将模型定义为某个类并将其绑定到该类。

答案 1 :(得分:1)

我从不使用linq to SQL,但在Entity Framework中我需要做这样的事情:

protected void ObjectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
        // Declaring the Entity context 
        AvalonEntities db = new AvalonEntities();

        // In updating méthod of a ObjectDataSource the "e" arg is my customer, so i grab this
        customer cus = (customer)e.InputParameters[0];


        // say that i have a DropDown to select the account ...
        // I Get de DropDown and get your SelectedValue.
        DropDownList ddlAccount  = (DropDownList)FormView1.FindControl("ddlAccount");
        // Whit the value i declare a new account
        account ac = new account () { id_account = int.Parse(ddlAccount.SelectedValue) };
        // and associate it to Customer
        cus.account = ac;

我希望这个帮助

答案 2 :(得分:0)

我已经成功从链接类中检索属性,如下所示:

First Name:
<asp:TextBox ID="FirstNameTextBox" runat="server"
 Text='<%# Bind("Entitycontact.Namefirst") %>' />