使用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();


似乎不会保存新记录吗?

最佳答案

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

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

09-25 16:18