From (1) We see that when the entity class definition is generated, the entity class or XML mapping file already contains the mapping information for entity and relational databases intact, Based on this information, Linq2sql translates the crud operations into SQL submissions to the database, and encapsulates the return DataTable of the database as the object we want.
A simple object is a entity class that has no foreign-key in the definition of a datasheet, and does not involve cascading operations when manipulating such objects.
CRUD operations for simple objects, refer to Msdn:http://msdn.microsoft.com/zh-cn/library/bb399349.aspx
Conveniently, when inserting data, linq2sql not only generates an INSERT SQL statement, but also generates a statement to retrieve the data columns of the Columnattribute tag isdbgenerated=true. This is especially handy when we use a database-generated uniqueidentifier column or a self-increasing ID key.
Let's discuss cascading operations and related issues together, for example I've defined two datasheets publishers and books:
Where PublisherID and BookID are rowguid, and the default value is newid (), the following code is based on SQLMetal-generated XML mapping and entity class.
Add to
The following code example adds two associated book records when you add a Publisher record
1 var context = GenerateContext();
2 Publisher publisher = new Publisher { Name = "Microsoft" };
3 publisher.Books.Add(new Book { Title = "Expert F#" });
4 publisher.Books.Add(new Book { Title = "Beautiful code" });
5
6 context.Publishers.InsertOnSubmit(publisher);
7 context.SubmitChanges();
The commit was successful, and the associated object was added as if it were a collection operation. But what seems to be missing? We don't seem to assign a value to the Book.publisherid, as a foreign key is not assigned a value why not throw an exception? This is all the credit for generating code, let's look at the definition of the Publisher.books attribute
1 private EntitySet<Book> _Books;
2
3 public Publisher()
4 {
5 this._Books = new EntitySet<Book>(
6 new Action<Book>(this.attach_Books), new Action<Book>(this.detach_Books));
7 OnCreated();
8 }