亚音速数据库对象的重用
另一个新手 SubSonic/ActiveRecord 问题。假设我想插入几条记录,目前我正在这样做:
using (var scope = new System.Transactions.TransactionScope())
{
// Insert company
company c = new company();
c.name = "ACME";
c.Save();
// Insert some options
company_option o = new company_option();
o.name = "ColorScheme";
o.value = "Red";
o.company_id = c.company_id;
o.Save();
o = new company_option();
o.name = "PreferredMode";
o.value = "Fast";
o.company_id = c.company_id;
o.Save();
scope.Complete();
}
但是,单步执行此代码,每个 company/company_option 构造函数都会创建一个新的 myappDB 对象,这似乎很浪费。
这是推荐的方法还是我应该尝试重用单个数据库对象 - 如果是这样,最简单的方法是什么?
Yet another newbie SubSonic/ActiveRecord question. Suppose I want to insert a couple of records, currently I'm doing this:
using (var scope = new System.Transactions.TransactionScope())
{
// Insert company
company c = new company();
c.name = "ACME";
c.Save();
// Insert some options
company_option o = new company_option();
o.name = "ColorScheme";
o.value = "Red";
o.company_id = c.company_id;
o.Save();
o = new company_option();
o.name = "PreferredMode";
o.value = "Fast";
o.company_id = c.company_id;
o.Save();
scope.Complete();
}
Stepping through this code however, each of the company/company_option constructors go off and create a new myappDB object which just seems wasteful.
Is this the recommended approach or should I be trying to re-use a single DB object - and if so, what's the easiest way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信如果您愿意,您可以通过将其
IsNew
属性设置为true
来使用同一对象,然后更改其数据属性,再次保存,重复。很容易。不过,我不太确定你是否应该打扰。这取决于这些构造函数对你的伤害有多严重。
I believe you can use the same object if you want to by setting its
IsNew
property totrue
, then change its data properties, save it again, repeat. Easy enough.I 'm not so sure that you should bother, though. It depends on how bad those constructors are hurting you.
在我看来,将多个对象分配给一个变量从来都不是一个好主意,但这是有争议的。我会这样做:
如果您担心性能,那不应该是问题,除非您想一次插入或更新许多对象。同样,在这种情况下,插入数据所花费的时间比创建对象所花费的时间要长。
如果您担心性能,可以使用插入查询完全跳过对象创建和保存部分:
http:// /www.subsonicproject.com/docs/Linq_Inserts
In my eyes assigning multiple objects to a single var is never a good idea but thats arguable. I would do this:
I you are worried about performance, that shouldn't be a issue unless you want to insert or update many objects at once. And again, in this case the time used for inserting the data takes longer than for the object creation.
If you are worried about performance you can skip the object creation and saving part completly by using a Insert query:
http://www.subsonicproject.com/docs/Linq_Inserts