The addtoxxxx () method is automatically generated when an entity is generated when an object is added. Therefore, this extension does not include the method for adding an object;
Demo
Public static class test
{
Public static void updatecustomer ()
{
Crmentityprovider crmep = newCrmentityprovider ();
// Here, the customer instance is usually deserialized in JSON/XML.
Customer customer =NewCustomer () {pk_customer = 123, name = "ABC "};
Crmep. updateentity (customer );
}
}
Public class Crmentityprovider { Private Type _ Contexttype; Public Crmentityprovider () {_ contexttype = Typeof ( Crmentities );}Public bool Updateentity <t> (T entity) Where T: system. Data. Objects. dataclasses. Entityobject {_ Contexttype. updateentity (entity ); Return true ;} Public bool Deleteentity <t> (T entity) Where T: system. Data. Objects. dataclasses. Entityobject {_ Contexttype. deleteentity (entity ); Return true ;}}
The following is an extension method class.
Public static class Objectcontextextension { /// <Summary> /// Add the entity to objectcontext /// </Summary> /// <typeparam name = "tentity"> </typeparam> /// <Param name = "entity"> </param> /// <Param name = "context"> </param> Private Static void Attachentity <tentity> (tentity entity, Objectcontext Context) Where Tentity:Entityobject { VaR Entitysetname = ( From P In Context. GetType (). getproperties () Where P. propertytype. fullname. Contains ( Typeof (Tentity). fullname) Select P). First (). Name; If (Entitysetname = Null ) Throw new Notsupportedexception ("Context has not an entity ." ); String Entityfullname = context. defaultcontainername + "." + Entitysetname; context. attachto (entitysetname, entity );} /// <Summary> /// Update only if the attribute value of entity is not null. /// </Summary> /// <typeparam name = "tentity"> </typeparam> /// <Param name = "contexttype"> </param> /// <Param name = "entity"> </param> Public static void Updateentity <tentity> ( This Type Contexttype, tentity entity) Where Tentity: Entityobject { If (! Typeof ( Objectcontext ). Isassignablefrom (contexttype )) Throw new Notsupportedexception ( "Contexttype is not an objectcontext ." ); Using ( Objectcontext Context = Activator . Createinstance (contexttype)As Objectcontext ) {Attachentity <tentity> (entity, context ); VaR Propertys = From P In typeof (Tentity). getproperties (system. reflection. Bindingflags . Instance | system. reflection. Bindingflags . Public) Where P. getgetmethod ( False )! = Null & P. getvalue (entity, Null )! = Null let Attribute = ( Edmscalarpropertyattribute ) Attribute . Getcustomattribute (p, Typeof ( Edmscalarpropertyattribute )) Where Attribute! = Null &&! Attribute. entitykeyproperty Select P. Name; system. Data. Objects. Objectstateentry Se = context. objectstatemanager. getobjectstateentry (entity );Foreach ( VaR Item In Propertys) {se. setmodifiedproperty (item);} context. savechanges ();}} /// <Summary> /// Delete /// </Summary> /// <typeparam name = "tentity"> </typeparam> /// <Param name = "contexttype"> </param> /// <Param name = "entity"> </param> Public static void Deleteentity <tentity> ( This Type Contexttype, tentity entity)Where Tentity: Entityobject { If (! Typeof ( Objectcontext ). Isassignablefrom (contexttype )) Throw new Notsupportedexception ( "Contexttype is not an objectcontext ." ); Using ( Objectcontext Context = Activator . Createinstance (contexttype) As Objectcontext ) {Attachentity <tentity> (entity, context); context. deleteobject (entity); context. savechanges ();}}