EF5 Frame Package

Source: Internet
Author: User

Last week, in the system, because it is a new computer, there is no use of the previous VS2010 for 2013, and then because the database table structure updated, so the EF generated entities are updated. Then the hand is a little bit over, the results found that the bottom of the operation is not chat. A look at the reason:accessbase<t> where t:entityobject.

What is the reason, just beginning to see or more dizzy, there is no problem ah, how can error. Then find the source code discovery, EF5 for the entity generated is class rather than the original entityobject.
public partial class Sysoperatelog. I don't know if this is an improvement or a way to regress. In LINQ, Microsoft is based on the class to do the relevant operation. I used to prefer LINQ, but I used EF for a long time. The framework for the previous EF4 is now updated as follows:

public bool Update<t> (T entity, String PrimaryKey, Object Primarykeyvalue) where T:class
{

Mainly for the underlying update method, because the previous entity:entityobject is able to find the primary key through the entity, it is certainly not.

Type type = typeof (T);
String strName = entities. Connection.ConnectionString.Replace ("Name=", "" ");
EntityKey key = null;
Try
{
Key = entities. Createentitykey (type. Name, entity);
}
catch (Exception ex)
{
throw new Exception ("Cannot find the primary key! ");
}

The primary key can be found directly through the above method.

All methods encapsulated:

[CSharp]View Plaincopy
    1. #region Update Entities
    2. Public bool Update<t> (T entity, string PrimaryKey, object primarykeyvalue) where T: class
    3. {
    4. Type type = typeof (T);
    5. string strName = entities.  Connection.ConnectionString.Replace ("name=", " " ");
    6. EntityKey key = null;
    7. Try
    8. {
    9. Key = entities. Createentitykey (type. Name, entity);
    10. }
    11. catch (Exception ex)
    12. {
    13. throw New Exception ("Cannot find the primary key!  ");
    14. }
    15. object propertyvalue = null;
    16. T entityfromdb = (t) entities. Getobjectbykey (key);
    17. if (null = = Entityfromdb)
    18. return false;
    19. propertyinfo[] properties1 = Entityfromdb.gettype (). GetProperties ();
    20. foreach (PropertyInfo property in properties1)
    21. {
    22. PropertyValue = null;
    23. if (null! = property. GetSetMethod ())
    24. {
    25. PropertyInfo Entityproperty =
    26. Entity. GetType (). GetProperty (property. Name);
    27. if (EntityProperty.PropertyType.BaseType = =
    28. Type.GetType ("System.ValueType") | |
    29. Entityproperty.propertytype = =
    30. Type.GetType ("System.String"))
    31. PropertyValue = entity. GetType (). GetProperty (property. Name).  GetValue (entity, null);
    32. if (propertyvalue = = null)
    33. {
    34. Thread.Sleep (50);
    35. PropertyValue = entity. GetType (). GetProperty (property. Name).  GetValue (entity, null);
    36. }
    37. if (null! = PropertyValue)
    38. {
    39. Try
    40. {
    41. String Name = property. Name;  //"Reference";
    42. if (Name.indexof ("Reference") < 0)
    43. {
    44. Property.  SetValue (Entityfromdb, propertyvalue, null);
    45. }
    46. }
    47. catch (Exception ex) {}
    48. }
    49. }
    50. }
    51. Entities. SaveChanges ();
    52. return true;
    53. }
    54. #endregion

EF5 Frame Package

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.