locked
Entity Validation RRS feed

  • Question

  • Hello,

    If I call GetValidationErrors() method, I don't get any results, even though I just added a new entity with a [Required] attribute applied to a property with a null value. If I try to save it, it does raise an exception.

    Is GetValidationErrors() not implemented?

    Thanks,

    RP

    Sunday, February 6, 2011 2:54 PM

Answers

  • Hi,

    Code First would look the same as what you posted, except that your code points to a connection string in your app/web.config file that points to xml files to load the mapping from. Code First doesn't load mapping from xml files you would typically override the OnModelCreating method in your context and do the mapping in code (Code First would do some of the mapping using conventions but you usually need to do some explicit configuration yourself). It's the process of building a model by scanning your classes (rather than parsing xml) where CTP5 discovers the attributes to validate against.

    ~Rowan

    • Proposed as answer by Fede Azzato Tuesday, March 15, 2011 4:24 PM
    • Marked as answer by Ricardo PeresMVP Tuesday, March 15, 2011 4:40 PM
    Thursday, February 17, 2011 6:26 PM
    Moderator

All replies

  • Hi,

    GetValidationErrors is implemented, any chance you can post up some code so we can see if this is a bug?

    ~Rowan

    Wednesday, February 9, 2011 7:53 PM
    Moderator
  • Sure:

    public partial class Blog

    {

    public int BlogId { get; set; }

    [StringLength(5, MinimumLength = 1)]

    [Required]

    public string Name { get; set; }

    ...

    }

    using (BlogEntities ctx = new BlogEntities())

    {

    Blog b = ctx.Blog.Create(); b.Name = null; ctx.Blog.Add(b);

    var errors1 = ctx.Entry(b).GetValidatio nResult(); //nothing

    var errors2 = ctx.GetValidationErrors(); //nothing

    ctx.SaveChanges(); //exception

    }


    Thanks,

    RP

    Wednesday, February 9, 2011 8:47 PM
  • Hi,

    Are you using Code First? I notice that your context has the name 'BlogEntities' which makes me think you are probably using the designer? In CTP5 validation is only performed in Code First, the next release will have support for Model and Database First.

    ~Rowan

    Wednesday, February 9, 2011 9:31 PM
    Moderator
  • Hello, Rowan!

    Well, I did generate my entities from the DB... but what is the difference? My context is still a DbContext:

     

    public partial class BlogEntities : DbContext
    {
    public BlogEntities()
    base("name=BlogEntities")
    {
    }

    public DbSet<Attachment> Attachment { getset; }
    public DbSet<Author> Author { getset; }
    public DbSet<AuthorDetails> AuthorDetails { getset; }
    public DbSet<Blog> Blog { getset; }
    public DbSet<Comment> Comment { getset; }
    public DbSet<Post> Post { getset; }
    public DbSet<Tag> Tag { getset; }
    }

     

    Regards,

    RP

    Wednesday, February 9, 2011 10:16 PM
  • Hi,

    The issue is that we add some additional information to the model during Code First model discovery and this information drives validation in CTP5. If the model comes from and EDMX file then this information isn't present so validation does not happen. This is a temporary restriction in CTP5 and validation will behave the same for all approaches in RTM.

    ~Rowan

    Wednesday, February 9, 2011 10:49 PM
    Moderator
  • I see... but, BTW, what would a code-first code look like? In what would it be different from the code I posted?

    Thanks,

    RP

    Wednesday, February 9, 2011 11:58 PM
  • Hi,

    Code First would look the same as what you posted, except that your code points to a connection string in your app/web.config file that points to xml files to load the mapping from. Code First doesn't load mapping from xml files you would typically override the OnModelCreating method in your context and do the mapping in code (Code First would do some of the mapping using conventions but you usually need to do some explicit configuration yourself). It's the process of building a model by scanning your classes (rather than parsing xml) where CTP5 discovers the attributes to validate against.

    ~Rowan

    • Proposed as answer by Fede Azzato Tuesday, March 15, 2011 4:24 PM
    • Marked as answer by Ricardo PeresMVP Tuesday, March 15, 2011 4:40 PM
    Thursday, February 17, 2011 6:26 PM
    Moderator