使用实体框架 1 (EF1) 进行更新 - 在 saveChanges() 上
我正在尝试增加存储在数据库中的计数器。
因此,这需要我使用实体框架 1 (EF1) 进行更新。
我正在做这样的事情:
CounterTBL OrderCounter = MyRepository.CounterTableDetails("ORDERID");
Booking Booking = new Booking();
Booking.BookingAdminID = User.ID;
Booking.BookingStatus = 2;
OrderCounter.CounterFLD = OrderCounter.CounterFLD + 1;
using (var ctx = new WhygoContext())
{
ctx.AddToBookings(Booking);
ctx.SaveChanges();
}
预订插入得很好,但我预计现有记录会被更新,但事实并非如此。
在 StackOverflow 和网络上的搜索表明我应该这样做: ctx.CounterTBL.Attach(OrderCounter); ctx.ApplyCurrentValues("CounterTBLs", OrderCounter);
或类似的,但我的智能感知不喜欢这个,它不会构建,所以我认为这些只是 EF 4 的一部分。
遗憾的是,我坚持使用 EF 1。有没有办法做到这一点。
我对这些东西还很陌生,所以也许我没有以正确的方式处理这件事......
I am trying to increment a counter which is stored in the DB.
So this requires me to do and update using Entity Framework 1 (EF1).
I am doing something like this:
CounterTBL OrderCounter = MyRepository.CounterTableDetails("ORDERID");
Booking Booking = new Booking();
Booking.BookingAdminID = User.ID;
Booking.BookingStatus = 2;
OrderCounter.CounterFLD = OrderCounter.CounterFLD + 1;
using (var ctx = new WhygoContext())
{
ctx.AddToBookings(Booking);
ctx.SaveChanges();
}
Booking is inserted fine, but I expected the existing record to be updated, which is was not.
A search around StackOverflow and the web shows that I should do something like this:
ctx.CounterTBL.Attach(OrderCounter);
ctx.ApplyCurrentValues("CounterTBLs", OrderCounter);
Or similar, but my intellisense doesn't like this and it doesn't build so I assume these are only a part of EF 4.
I am sadly stuck with EF 1. Is there a way to do this.
I'm pretty new to this stuff, so maybe I'm not going about this in the right way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您插入
Booking
时,您正在创建上下文的新实例,并仅在该实例上调用保存更改。您的OrderCounter
是从存储库加载的,我猜它使用了不同的上下文实例。您应该在两个操作之间共享上下文实例,否则您必须在两个上下文上调用SaveChanges
。顺便提一句。如果您的代码在 ASP.NET 中运行,则它不是很可靠,因为并发客户端可以存储相同的计数器。
When you're inserting
Booking
you are creating a new instance of the context and call save changes only on that instance. YourOrderCounter
was loaded from repository and I guess it used different context instance. You should share the context instance between both operations or you will have to callSaveChanges
on both context.Btw. your code is not very reliable if it is run in ASP.NET because concurrent clients can store the same counter.