Overwriting the default value of a type
Hi,
Is it possible to overwrite the default value given to a type? If not, can this be implemented?
It would be very handy for the nullable Guid type. These properties are now set with the guid value '00..1', which gives a FK exception when saved to a database. I have to fix this by setting null to each Guid? type property.
Example: the ParentId property is the foreign key (nullable Guid) for the 'Parent' property:
var productStructure = Builder<Domain.ProductStructure>.CreateNew()
.With(ps => ps.Level = level)
.With(ps => ps.ParentId = null)
.Build();
@ArnaudB88 I realize this is an extremely late response, but perhaps it may be helpful for someone else who comes along.
I just took a stab at implementing this. In doing so, I found that it seems you may be able to accomplish this by using the following code.
BuilderSetup.DisablePropertyNamingFor<Domain.ProductStructure, Guid?>(x => x.ParentId);
By doing so, a default value is not generated for the ParentId property.
Suppose I have the following class
public class NBuilderDummy
{
public string Foo { get; set; }
public Guid? Bar { get; set; }
public int? Baz { get; set; }
}
BuilderSetup.DisablePropertyNamingFor<NBuilderDummy, Guid?>(x => x.Bar);
var nullableGuidSample = Builder<NBuilderDummy>.CreateNew().Build();
Console.WriteLine($"{JsonConvert.SerializeObject(nullableGuidSample, Formatting.Indented)}");
outputs
{
"Foo": "Foo1",
"Bar": null,
"Baz": 1
}
If you omit the first line with DisablePropertyNamingFor then the output is
{
"Foo": "Foo1",
"Bar": "00000000-0000-0000-0000-000000000001",
"Baz": 1
}
@Doug-Murphy I was aware that it was possible with that method, but with a huge database and many nullable Guids it is not exactly user-friendly. Also, changing the database would imply you must change the nbuilder setup. Therefor I would suggest disabling the default value based on a type.
Thank you for looking into this! :)
Arnaud,
That's what I was thinking. :) Which is why I implemented what I did yesterday. Feel free to check out the PR I opened up yesterday to see if that would satisfy your use case.
On Mon, Jan 6, 2020, 6:34 AM ArnaudB88 [email protected] wrote:
@Doug-Murphy https://github.com/Doug-Murphy I was aware that it was possible with that method, but with a huge database and many nullable Guids it is not exactly user-friendly. Also, changing the database would imply you must change the nbuilder setup. Therefor I would suggest disabling the default value based on a type.
Thank you for looking into this! :)
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nbuilder/nbuilder/issues/86?email_source=notifications&email_token=AAKRXMYUSDWCRD7DV4PSULDQ4MJNJA5CNFSM4FJ3XU42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIFGOMA#issuecomment-571107120, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKRXM33MHQ2UDVRZGCVDMTQ4MJNJANCNFSM4FJ3XU4Q .
Just went through the PR, looks very good! Thank you for this :D I hope it gets approved soon.
I'm glad that it fits your use case! :) My last PR went in after 2 weeks or so I think? And it got updated on NuGet not long after. Stay on the lookout I guess. :)
In the meantime, should this issue be closed?
On Mon, Jan 6, 2020, 6:51 AM ArnaudB88 [email protected] wrote:
Just went through the PR, looks very good! Thank you for this :D I hope it gets approved soon.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nbuilder/nbuilder/issues/86?email_source=notifications&email_token=AAKRXM4AYBBUV4YZMRHWYGDQ4MLMTA5CNFSM4FJ3XU42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIFHP3Y#issuecomment-571111407, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKRXM6OQA3MDZN6ZUEHS5DQ4MLMTANCNFSM4FJ3XU4Q .
IMO, nope ... leave this open. Using the Github closing commit keywords, when the PR is merged, it will auto closed linked issues, now that i've linked the two.
I see that the PR is merged into the develop branch. Is it possible to merge it to the master branch and release a new nuget version?
@crmckenzie
@Doug-Murphy @crmckenzie Can anyone look into this please.
It very silent in this repository ... Maybe it is an idea to add owners? (just a suggestion)
@ArnaudB88 another suggestion (due to the silent repo) is to possibly look at using https://github.com/bchavez/Bogus as a replacement for this repo.
@ArnaudB88 I agree. :) It seems that this repo has died off. As such, I have decided to stop contributing.
As @PureKrome said, I think you may be better served using a different package moving forward.
yeah - maybe this repo should go into archive mode and suggest people to goto Bogus, instead.
Hi - I'm the original author. I haven't used .NET for many years so this project is not something that I can justify spending any time on and I'm out of touch with it now anyway, but I'm happy to appoint someone to look after the repository if anyone wishes.