markrendle / Simple.Data

A light-weight, dynamic data access component for C# 4.0
MIT License
1.33k stars 303 forks source link

Having dictionary as property in POCO throws a casting error. Does not get initialized as null #344

Open terzano opened 10 years ago

terzano commented 10 years ago

When a POCO has a dictionary a casting error will be thrown. Lists and other scalar properties not contained in the query will be initialized with their defaults values (Lists are correctly initialized as null) but Dictionaries won't.

Error:

Additional information: Cannot implicitly convert type 'Simple.Data.SimpleRecord' to Customer.

Example: Let's say my POCO looks like this..

  public class Customer
  {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public IDictionary<AddressType, Address> Addresses { get; set; }

        public string FullName
        {
            get
            {
                return string.Concat(LastName, ", ", FirstName);
            }
        }

        public Customer()
        {
            this.Addresses = new Dictionary<AddressType, Address>();
        }
  }

Then in my DAL I have something like this... _db is the dynamic Simple.Data database object.

Let's assume that my Customer table only has the following columns: id, firstname and lastname.

public IEnumerable<Customer> GetCustomers()
{
    return _db.dbo.Customer.All()
            .ToList<Customer>();
}