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
- #region Update Entities
- Public bool Update<t> (T entity, string PrimaryKey, object primarykeyvalue) where T: class
- {
- 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! ");
- }
- object propertyvalue = null;
- T entityfromdb = (t) entities. Getobjectbykey (key);
- if (null = = Entityfromdb)
- return false;
- propertyinfo[] properties1 = Entityfromdb.gettype (). GetProperties ();
- foreach (PropertyInfo property in properties1)
- {
- PropertyValue = null;
- if (null! = property. GetSetMethod ())
- {
- PropertyInfo Entityproperty =
- Entity. GetType (). GetProperty (property. Name);
- if (EntityProperty.PropertyType.BaseType = =
- Type.GetType ("System.ValueType") | |
- Entityproperty.propertytype = =
- Type.GetType ("System.String"))
- PropertyValue = entity. GetType (). GetProperty (property. Name). GetValue (entity, null);
- if (propertyvalue = = null)
- {
- Thread.Sleep (50);
- PropertyValue = entity. GetType (). GetProperty (property. Name). GetValue (entity, null);
- }
- if (null! = PropertyValue)
- {
- Try
- {
- String Name = property. Name; //"Reference";
- if (Name.indexof ("Reference") < 0)
- {
- Property. SetValue (Entityfromdb, propertyvalue, null);
- }
- }
- catch (Exception ex) {}
- }
- }
- }
- Entities. SaveChanges ();
- return true;
- }
- #endregion
EF5 Frame Package