Use guid with caution in Entity Framework?

Source: Internet
Author: User

I recently encountered a series of strange problems when using EF and data service, probably because I used guid as the foreign key of the primary key.

For example:

VaR C2 = Dc. categories. Where (O => O. categoryid = C. categoryid). First ();

Categoryid is of the long type and can run normally.

And:

VaR customers = Dc. Customers. Where (O => O. customerid = new GUID ("4c65d18f-9ff1-4c31-accd-1fea164ef507 "));

If customerid is of the guid type, no data is found.

Must be changed:

Guid gid = new GUID ("4c65d18f-9ff1-4c31-accd-1fea164ef507 ");
VaR customers = Dc. Customers. Where (O => O. customerid = GID );

The following are some references:

Http://msdn.microsoft.com/en-us/library/dd283139.aspx

Http://geekswithblogs.net/SudheersBlog/archive/2009/06/11/132758.aspx

In addition, when GUID is used as the foreign key type, you cannot add the data in the detail table correctly. The following table structure:

Product

Productid Guid
Categoryid Guid
Productname String

Category

Categoryid Guid
Categoryname String

Write the following code:CodeCannot run correctly:

Dataservicecontext Dc = new dataservicecontext ();

Category C = combobox1.selecteditem as category;

Product P = new product ()

{

Category = C,

Productname = "some good"

}

DC. addtoproduct (P );

DC. setlink (p, "category", p. Category );

DC. savechanges ();

If you change the data type of the preceding ID to string or number, the above Code runs correctly.

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.