Environment: vs2008, silverlight3
Domainsevice does not need to be updated or deleted in most cases.Code
However, it seems that some situations are not very good. For example, I want to update or delete a record using the key value.
The Silverlight side is an operation on entity, and is added, modified, or removed based on entity. It triggers domainsevice to call the corresponding insert, update, and delete operations through submitchanges, when entity has been loaded to domaincontext, this operation is very convenient and the logic is clear.
But if we encounter such an example, such as Gmail labels, general design, there will be three table letters, the tag master (store the tag information, there will be different types of labels here, it may not be the mail tag), the tag detail (which mail or other materials are stored here)
When loading data, the mail and the tag master need to be loaded, and the tag detail does not need to be loaded. In this case, to remove a tag of a letter, the problem arises, you need to load it to the Silverlight end, remove it, and then submitchanges ()
For example, the website statistics only require updating the statistics and Load
I use attach and custom methods for domain operations to solve these problems. I hope to help people who need them and share your methods with anyone who wants a better method.
Method 1 attach
The web side is not changed, and the code is written on the Silverlight side
Delete:
Tagcontext xtagcontext = new tagcontext ();
Tagdetail xtagdetail = new tagdetail () {uniid = "here is the key value"}; the primary key of the data to be deleted must be initialized. Regular Primary keys are convenient.
Xtagcontext. tagdetails. Attach (xtagdetail );
Xtagcontext. tagdetails. Remove (xtagdetail );
Xtagcontext. submitchanges ();
Update:
Tagcontext xtagcontext = new tagcontext ();
Tagdetail xtagdetail = new tagdetail () {uniid = "here is the key value"}; the primary key of the data to be updated must be initialized
Xtagcontext. tagdetails. Attach (xtagdetail );
Xtagdetail... =... // update method, written after attach
Xtagcontext. submitchanges ();
Method 2 custom domain Operation Method
The domainservice method must comply with the Conventions to generate the domaincontext on the Silverlight end.
Delete:
Domainservice write custom Method
[Enableclientaccess ()]
Public class tagservice: linqtoentitiesdomainservice <smbsysentities>
{
// Write the custom method in the corresponding domainservice
// There are two points to note: 1. The method name cannot start with delete or remove.
// 2. parameter type, which must be T: entity (inheriting the entity type) or string, int basic types (Haha, I am not very clear about the type conventions here, just try)
Public void deltagdetail (string uniid) // you can modify the parameters as needed.
{
// Here you can write the processing method by yourself. You can use traditional SQL statements and LINQ statements (Attach is also used here)
Tagdetail xtagdetail = new tagdetail () {uniid = uniid}; the primary key of the data to be deleted must be initialized. Regular Primary keys are convenient.
This. objectcontext. Attach (xtagdetail );
This. objectcontext. deleteobject (xtagdetail );
This. objectcontext. savechanges ();
}
}
Silverlight call:
Tagcontext xtagcontext = new tagcontext ();
Invokeoperation IO = xtagcontext. deltagdetail ("input parameter ");
Io. Completed + = ..
Update:
Domainservice write custom Method
[Enableclientaccess ()]
Public class tagservice: linqtoentitiesdomainservice <smbsysentities>
{
// Write the custom method in the corresponding domainservice
// Note the following two points: 1. The method name cannot start with update, change, or modify.
// 2. parameter type, which must be T: entity (inheriting the entity type) or string, int basic types (Haha, I am not very clear about the type conventions here, just try)
Public void upttagdetail (string uniid) // you can modify the parameters as needed.
{
// Here you can write the processing method by yourself. You can use traditional SQL statements and LINQ statements (Attach is also used here)
Tagdetail xtagdetail = new tagdetail () {uniid = uniid}; the primary key of the data to be updated must be initialized. Regular Primary keys are convenient.
This. objectcontext. Attach (xtagdetail );
Xtagdetail... =... // update content
This. objectcontext. savechanges ();
}
}
Silverlight call:
Tagcontext xtagcontext = new tagcontext ();
Invokeoperation IO = xtagcontext. upttagdetail ("input parameter ");
Io. Completed + = ..