Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. How do you use DataAnnotations in WCF

How do you use DataAnnotations in WCF

Scheduled Pinned Locked Moved WCF and WF
csharpwcfagentic-aiannouncement
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sevententh
    wrote on last edited by
    #1

    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);
            }
        }
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups