How do you use DataAnnotations in WCF
WCF and WF
1
Posts
1
Posters
0
Views
1
Watching
-
Hi I've been struggling with this all week, validation on EDM in a WCF service (using basicHttpBinding). Ideally I'd like to use DataAnnotations as it seems easier/quicker to code when I have 100+ tables that all require some validation on the fields. The following code works using On<FieldName>Changing but I can't get the same to work with DataAnnotations.
public partial class tblCfg\_Agent { partial void OnAgentCodeChanging(string value) { if (value == String.Empty) { throw new ValidationException("Agent must have a code"); } if (value.Length > 5) { throw new ValidationException("Agent Code must be no more than 5 characters"); } } }
But if I want to use the following no validation happens
\[MetadataType(typeof(tblCfg\_AgentValidation))\] public partial class tblCfg\_Agent {} public class tblCfg\_AgentValidation { \[Required(ErrorMessage = "Agent must have a code")\] \[StringLength(5, ErrorMessage = "Agent Code must be no more than 5 characters")\] public string AgentCode { get; set; } }
Could somebody spot what I'm doing wrong please :sigh: Update code
/// <summary> /// Updates an existing tblCfg\_Agent /// </summary> public void Update(tblCfg\_Agent ds) { try { EntitiesContext context = new EntitiesContext(); context.AttachUpdated(ds); context.SaveChanges(); } catch (OptimisticConcurrencyException e) { // someone else changed the data, throw throw (e); } }