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.