如何使用Entity Framework和C#将变量值插入数据库?

时间:2017-11-08 16:21:02

标签: c# entity-framework-6

我想将一堆变量的值插入数据库,我该怎么做?

我有这段代码,但无法弄清楚如何使用变量。

  private void button2_Click(object sender, EventArgs e)
    {
        var context = new ASH_ORDER_DBEntities();

        var t = new ASH_PROD_ORDERS //Make sure you have a table called test in DB
        {
            Creator = "", Product = "", Created = "", Qty = "", Part_Number = "", Due = "", Edited = "", Ordered = null, Other = "", Urgent = null, Supplier = "",
        };
        context.ASH_PROD_ORDERS.Add(t);
        context.SaveChanges();
    }

1 个答案:

答案 0 :(得分:2)

你是说这个意思吗?这似乎是一个奇怪的问题 - 你有一个EF上下文而不知道如何声明一个局部变量?

private void button2_Click(object sender, EventArgs e)
{
    string myCreator = CreatorTextBox.Text;
    string myProduct = ProductTextBox.Text;
    int myQty = QuantityEdit.Value;
    //....more vars...

    var t = new ASH_PROD_ORDERS
    {
        Creator = myCreator, 
        Product = myProduct, 
        Qty = myQty, 
        //... more 'Property = var' lines
    };
    context.ASH_PROD_ORDERS.Add(t);
    context.SaveChanges();
}