LINQ those Things (2)-crud operations of simple objects and cascading operations of association

Source: Internet
Author: User
Tags class definition

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 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.