Search code examples
c#sqlitedapperidataerrorinfo

How to use Dapper with IDataErrorInfo Interface validations?


I am new to ORM so please be calm to answer my question. I have built a POCO admin class with Dapper attributes and Implemented IDataErrorInfo for validations on my Model classes Here:

public partial class Admin:IDataErrorInfo {

    [Key]
    public long Admin_Id { get; set; }
    public string Admin_Name { get; set; }
    public string Password { get; set; }
    public virtual IEnumerable<Verifying_Agent> Verifying_Agent { get; set; }

    public string Error
    {
        get { throw new NotImplementedException(); }
    }
    public string this[string columnName]
    {
        get
        {
            switch (columnName)
            {
                case "Admin_Name":
                    if (string.IsNullOrEmpty(Admin_Name))
                        return "Admin Name is required";
                    break;
                case "Password":
                    if (string.IsNullOrEmpty(Password))
                        return "Password is Required";
                    break;

            }
            return "";
        }
    }
}

But when i excute Dapper.Simple CRUD library for Simple CRUD operation i.e

conn= new SQLiteConnection("Data Source=" + Environment.CurrentDirectory + "\\SystemDB.db");
 var admit = conn.Get<Admin>("select * from Admin");

i get the following Error "Column or Database not exist"

While if I remove IDataErrorInfo implementaion the Error go away. Is there something i am missing while using Dapper or there is a bug in This library?


Solution

  • I don't believe that there is a bug in the library and I've been unable to reproduce your problem despite creating an almost identical class and performing a similar query against a SQLite database.

    This is the class I used:

    class Post : IDataErrorInfo
    {
        [Key]
        public int Id { get; set; }
        public string Title { get; set; }
        public string Markdown { get; set; }
    
        public string Error
        {
            get { throw new NotImplementedException(); }
        }
        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "Title":
                        if (string.IsNullOrEmpty(Title))
                            return "Title is required";
                        break;
                    case "Markdown":
                        if (string.IsNullOrEmpty(Markdown))
                            return "Markdown is required";
                        break;
                }
                return "";
            }
        }
    }
    

    and this is how I queried the db:

    using (var conn = new SQLiteConnection("Data Source=" + Environment.CurrentDirectory + "\\Blog.sqlite"))
    {
        var posts = conn.Query<Post>("SELECT * FROM Posts");
        Console.WriteLine(posts.Count() + " post(s) retrieved");
    }
    

    I'm not sure what's causing your problem but I'm fairly sure that it's not Dapper.

    I'm mystified as to why removing the IDataErrorInfo interface would affect this error:

    Column or Database not exist

    That sounds like the query should fail regardless of the type that you're trying to populate.