gpt4 book ai didi

c# - 在数据库中创建新记录

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:12 26 4
gpt4 key购买 nike

我想使用 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

关于c# - 在数据库中创建新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21700269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com