Saving and updating (SQL Server rows) using SubSonic 3.0 and LINQ
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 te开发者_如何学运维mplates 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);
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:
var product = new Product(p => p.Id == 123);
For inserting:
var product = new Product();
product.RegionID = 6;
product.RegionDescription = "Hawaii";
product.Save();
Updating:
product.UnitPrice = 100;
product.ProductName = "Test";
product.Save();
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?
精彩评论