Entity is updated in. Net 3.5, and the method for deleting data is extended.

Source: Internet
Author: User
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 ();}}

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.