0

I use a static datacontext for CRUD operation.

When I create some records in the gridview, then delete some of them, and then update the database, the deleted records are also inserted.

I guess maybe the deleted records are deleted from the gridview1.datasource, but they are not deleted from the datacontext.

But how can I deal with it?

Thanks very much.

code:

public class PurchaseOrderService{
private static readonly BaseEntityDao<PurchaseOrder> ObjDataAccess = new BaseEntityDao<PurchaseOrder>();
    public static void InsertRecord(PurchaseOrder localTable)
        {
            ObjDataAccess.Add(localTable);
        }
}


public class BaseEntityDao<TEntity>
        where TEntity : class, new()
    {
        public void Add(TEntity entity)
        {
            NBDataLinqDataContext.Instance().GetTable<TEntity>().InsertOnSubmit(entity);
            SubmitChanges(NBDataLinqDataContext.Instance());
        }
    }

partial class NBDataLinqDataContext
    {
private static NBDataLinqDataContext instance;
public static NBDataLinqDataContext Instance()
        {
            // Uses "Lazy initialization"
            if (instance == null)
                instance = new NBDataLinqDataContext();
            instance.Log = Console.Out;
            return instance;
        }
}

winform code:

private void btnConfirm_Click(object sender, EventArgs e)
        {
var po = new PurchaseOrder();
foreach (PurchaseOrderDetail pd in _list)
                        {
                            po.PurchaseOrderDetail.Add(pd);
                        }
PurchaseOrderService.InsertRecord(po);
}
4

0 回答 0