使用 SubSonic 3.0 和 LINQ 保存和更新(SQL Server 行)
我觉得问这样一个基本问题很愚蠢,但不问会更愚蠢:)
我已经使用 SubSonic 2.x 多年并且喜欢它(感谢 Rob 和同事)。
我已经开始使用 SubSonic 3.0.0.4 进行试点项目,并选择了 LINQ T4 模板(如果我的术语有误,请原谅)。
现在我是 LINQ 的新手,但我正在研究如何构建查询。
我真正困扰的是如何使用新工具包创建和更新数据。
以前,它非常简单,我可以:
- “新建”一个对象,如果我没有对象
- ,或者通过 id
- 设置一些属性
- 来获取或构造一个现有对象,然后“保存()”,这
太棒了,节省了我几个小时。
现在在新工具包中,有些事情似乎是相同的,例如:
- 创建一个对象
- 设置一些属性,
但现在有些事情似乎更难,例如:
- 加载对象似乎是:
db.Product.FirstOrDefault(x => ProductID.Id == 123);
- 插入似乎是:
db.Insert.Into
( x => x.RegionID, x => x.区域描述) .Values(6, "夏威夷").Execute();
- 更新对象似乎更加困难:
db.Update<产品>() .Set(x => x.UnitPrice == 100, x => x.ProductName == "测试") .Where(x => x.ProductID == 1).Execute();
文档 (http://subsonicproject.com/docs/Linq_Updates) 讨论了如何使用存储库,但它不存在/无法通过我正在使用的 T4 模板生成。
因此,任何帮助让我知道:
- 我显然设置了错误,
- 我不知道我在做什么,方法是......在此处插入答案,
- 我理解正确,应该把它吸干,
这将是很大的帮助赞赏。
如果需要更多信息,请告诉我。
提前致谢。
马克
---- 更新 ----
总结 Denis 的答案:
LINQ 是一种查询语言,而不是 ORM
您可以通过以下方式获取 Repository 对象:
var repo = new SubSonic.Repository.SubSonicRepository(db);
I feel foolish asking such a fundamental question but it would be more foolish not to ask :)
I have been using SubSonic 2.x for years and love it (Thanks Rob and co.).
I have started a pilot project using SubSonic 3.0.0.4 and have chosen the LINQ T4 Templates (excuse me if I have the terminology wrong).
Now I am new to LINQ but I am doing OK working out how to build the queries.
What I am REALLY struggling with is how to create and update data with the new toolkit.
Previously it was super simple where I could:
- 'new' an object if I didn't have one
- or Fetch or construct an existing one by id
- set some properties
- then 'Save()' it
Fantastic and saved me hours.
Now in the new toolkit some things seem to be the same like:
- creating an object
- setting some properties
but right now some things seem a lot harder like:
- Loading an object seems to be:
db.Product.FirstOrDefault(x => ProductID.Id == 123);
- Inserting seems to be:
db.Insert.Into<Northwind.Region>( x => x.RegionID, x => x.RegionDescription) .Values(6, "Hawaii").Execute();
- Updating an object seems even harder:
db.Update<Product>() .Set(x => x.UnitPrice == 100, x => x.ProductName == "Test") .Where(x => x.ProductID == 1).Execute();
The documentation (http://subsonicproject.com/docs/Linq_Updates) talks about using the Repository but that just doesn't exist/get generated with the T4 templates I am using.
So any help to let me know that:
- I have obviously set this up wrong
- I have no idea what I am doing and the way to do it is ... insert answer here
- I understand it correctly and should just suck it up
would be greatly appreciated.
If any more info is needed please let me know.
Thanks in advance.
Mark
---- Update ----
To summarise the answer from Denis:
LINQ is a query language not an ORM
You can get to the Repository object via:
var repo = new SubSonic.Repository.SubSonicRepository(db);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯.. 我也将我的一个项目迁移到 SubSonic 3,但坦率地说,在保存实体时没有遇到任何麻烦。我使用相同的 ActiveRecord 技术,它仍然对我有用。例如,要检索我发出的实体:
对于插入:
更新:
如您所见,您仍然可以使用相同的
Save()
方法。所以,不太确定为什么你尝试通过“db”上下文来做到这一点,即使它也可以使用:)我错过了什么吗?
Hmm.. I migrated one of my projects to the SubSonic 3 too but frankly have not had any troubles saving the entities. I use the same ActiveRecord techniques and it still works great for me. For example, to retrieve an entity I am issuing:
For inserting:
Updating:
As you can see you can still use the same
Save()
method.So, not really sure why you try to do that through the 'db' context even though it can be used too :) Am I missing something?