bchavez / RethinkDb.Driver

:headphones: A NoSQL C#/.NET RethinkDB database driver with 100% ReQL API coverage.
http://rethinkdb.com/api/java
Other
384 stars 134 forks source link

[JsonIgnore] data returned is ignored. #35

Closed mrinc closed 8 years ago

mrinc commented 8 years ago

Hello,

I'm not sure you could call this an issue on your side but I wanted to state it so if it's something to keep note of, we could add it to the wiki.

If you create class and set a property as a JsonIgnore property, it does not get set when you select from the DB.

Class:

public class User
{
  [JsonIgnore]
  public Guid id { get; set; }
  public string name { get; set; }
}

Select:

var all = _r.Db("myDB")
                    .Table("Users")
                    .Run<User>(conn);

Result:

[0] User()
       id = {00000000-0000-0000-0000-000000000000}
       name = "John"

Thanks :+1:

bchavez commented 8 years ago

Hi @mrinc ,

Thanks for bringing this up.

I just want to confirmid is Guid.Empty because [JsonIgnore] is ignoring the field during _desalinization_ correct? I think DataExplroer should show the User with a non-empty GUID right?

So, I think the proper attribute for Id something like this:

public class User
{
    [JsonProperty("id", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

The id field is excluded during serialization on the way out, id is populated by RethinkDB on insert. Then, during deserialization on the way in, the id value is a non-empty GUID and populated.

This is probably what needs to be documented right?

bchavez commented 8 years ago

Hi @mrinc ,

I've added some more explicit documentation regarding POCO primary keys.

https://github.com/bchavez/RethinkDb.Driver/wiki/Extra-C%23-Driver-Features#poco-primary-keys

I hope this helps!

Thanks, Brian