在db中创建新记录

时间:2014-02-11 11:12:47

标签: c# asp.net asp.net-mvc-3

使用asp.net mvc项目我想创建一个新的ClientAccountAccess记录。

private static void SetAccessCode(string guidCode)
        {
            using (EPOSEntities db = new EPOSEntities())
            {
                ClientAccountAccess client = new ClientAccountAccess();
                client.GUID = guidCode;
                db.SaveChanges();
            }
        }

        //have also tried 
        //ClientAccountAccess client = new ClientAccountAccess()
        //{
        //    GUID = guidCode
        //};
        //db.SaveChanges();

似乎没有保存新记录?

1 个答案:

答案 0 :(得分:4)

您需要将此记录添加到上下文中:

private static void SetAccessCode(string guidCode)
{
    using (EPOSEntities db = new EPOSEntities())
    {
        ClientAccountAccess client = new ClientAccountAccess();
        client.GUID = guidCode;

        // ClientAccountAccess is name of your DBSet in context. It might be different
        db.ClientAccountAccess.AddObject(client); 
        db.SaveChanges();
    }
}

以下是基本CRUD操作的更多示例 - http://www.dotnetcurry.com/showarticle.aspx?ID=619