Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object reference not set to an instance of an object when property is nullable in the model #44

Open
darioMe opened this issue Oct 23, 2020 · 1 comment

Comments

@darioMe
Copy link

darioMe commented Oct 23, 2020

Hi

I would like to use this Mongo.migration library, but I have a small problem with nullable properties in a complex model.
For example if I have a main model Person:

[RuntimeVersion("0.0.0")]
public class Person : IDocument
{
     public Person(ObjectId id, string name, Partner? partner)
     {
          Id = id;
          Name = name;
          Partner = partner;
     }
        
     public ObjectId Id { get;  }
     public string Name { get;  }
     public Partner? Partner { get; }
     public DocumentVersion Version { get; set; }
}

which contains the model Partner, which can be null. And which is also derived from IDocument and has the versions field like for example:

[RuntimeVersion("0.0.0")]
public class Partner : IDocument
{
     public Partner(string name)
     {
          Name = name;
     }

     public string Name { get; }
     public DocumentVersion Version { get; set; }
}

Now if I want to save a person with an empty partner, I get an Object reference not set to an instance of an Object.
This is because Mongo.Migration in the DocumentVersionService wants to write the version to the partner, which is not instantiated. In the case, if i save a person with an partner, it works fine...

In the DocumentVersionService on the method DetermineVersion I added a nullable check for the instance. If it's null, it will return without writing the Version on the instance, and if the instance is not null it will add the version as is. This works for me...

Now my question, is this in the sense of Mongo.migration, that each model in a complex model has his own version field,
or should I add this version field only on the main model (Person) and then interact in the up or down migration script through the BsonDocument.
The advantage, when every modal has his own version would be, that the up and down in the migration script will be much easer to handle.

thanks for your answer...

@SRoddis
Copy link
Owner

SRoddis commented Sep 28, 2021

Hey @darioMe,

sorry for late response. I will have a look at this case.

But for now i would say, that you should just have a DocumentVersion on the base document and handel the change in your migration.

I will think about this case and will send you some informations.

Cheers,
Sean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants