开发者

Gridview ObjectDataSource InvalidOperationException when Deleting

I'm learning LINQ/EF and I have coded the following snippets. Create/Read/Update work but Delete throws an InvalidOperationException. When I mouseover the error it shows null values for the Customer object.

Any thoughts?

HTML CODE

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="CustomersODS">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    </Columns>
</asp:GridView>
<asp:ObjectDataSource ID="CustomersODS" runat="server" 
    DataObjectTypeName="CustOrderTest.Models.Customer" 
    DeleteMethod="Customer_Delete" SelectMethod="Customers_GetAll" 
    TypeName="CustOrderTest.Models.CustomerModel" UpdateMethod="Customer_Update">
</asp:ObjectDataSource>

Method Repository

namespace CustOrderTest.Models
{
    public class CustomerModel
    {

        COContext DB = new COContext();

        public List<Customer> Customers_GetAll()
        {
            return (from customer in DB.Customers
                    select customer).ToList();
        }

        public List<Customer> Customers_Search(string CustomerName)
        {
            return (from customer in DB.Customers
                    where customer.Name.Contains(CustomerName)
                    select customer).ToList();
        }

        public Customer Customer_GetByID(int? ID)
        {
            return (from customer in DB.Customers
                    where customer.ID == ID
                    select customer).FirstOrDefault();
        }

        public void Customer_Update(Customer customer)
        {
            DB.Entry(customer).State = EntityState.Modified;
            DB.SaveChanges();
        }

        public void Customer_Insert(Customer customer)
        {
            DB.Customers.Add(customer);
            DB.SaveChanges();
        }

        public void Customer_Delete(Customer customer)
        {
            DB.Customers.Remove(customer);
            DB.SaveChanges();
        }


    }
}

Data Model //

namespace CustOrderTest.Models
{
    public class Customer
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class CustomerOrder
    {
        public int ID { get; set; }
        public Customer Customer { get; set; }
        public DateTime OrderDate { get; set; }
        public开发者_运维百科 int MyProperty { get; set; }
        public List<CustomerOrderDetail> OrderDetails { get; set; }
    }

    public class CustomerOrderDetail
    {
        public int ID { get; set; }
        public string Part { get; set; }
        public int Quantity { get; set; }
        public double Price { get; set; }

    }

    public class COContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
        public DbSet<CustomerOrder> CustomerOrders { get; set; }
        public DbSet<CustomerOrderDetail> OrderDetails { get; set; }
    }

}


Foolish mistake - here is the answer. If you get this error its most likely that you didnt specity the DataKeyNames

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="CustomersODS">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜