Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

1 to many unidirectionnal association #263

Closed
pas059 opened this issue Feb 1, 2021 · 5 comments
Closed

1 to many unidirectionnal association #263

pas059 opened this issue Feb 1, 2021 · 5 comments
Labels
bug Confirmed bug released Issue is resolved in a current release
Milestone

Comments

@pas059
Copy link

pas059 commented Feb 1, 2021

Hello,

I notice errors in code generation with the '1 to many unidirectional association's. For instance, gieven an entity Organization and an entity Product, and an association 1 to many from Organization to Product, the generated constructor for Product is:

      public Product(global::EfDesigner1.Organization _organization0)
      {
         if (_organization0 == null) throw new ArgumentNullException(nameof(_organization0));
         this._organization0 = _organization0;
         _organization0.Products.Add(this);

         Init();
      }

The line this._organization0 = _organization0; is generated, but not a class member this._organization0. With EFDesigner 2.x this line is not generated. I think that this line should not be generated, or eventually that the corresponding data member be generated.

Regards

@msawczyn msawczyn added the investigating Looking into this label Feb 4, 2021
@Beast76
Copy link

Beast76 commented Feb 9, 2021

If I change the Multiplicity to "0..1 (Zero or one of 'xxx')" the error does no exists.

When I change it to the Bidirectional Association with the same Multiplicity, then the code will be generated correctly.

Kind regards

@msawczyn
Copy link
Owner

@pas059 and @Beast76 - since this is a code generation issue, which version of Entity Framework are you targeting?

@msawczyn msawczyn added bug Confirmed bug and removed investigating Looking into this labels Feb 11, 2021
@msawczyn msawczyn added this to the 3.0.4 milestone Feb 11, 2021
@msawczyn msawczyn added the pending release Issue is resolved in the current codebase, will be published with the next release label Feb 11, 2021
@msawczyn
Copy link
Owner

Heh. Doesn't matter - it's pervasive. I found the issue and it'll be fixed in 3.0.4.

In the meantime, if it's blocking you, you can add EFModelGenerator.ttinclude to your project and change the code starting at line 708 to:

               // all required navigation properties that aren't a 1..1 relationship
               foreach (NavigationProperty requiredNavigationProperty in modelClass.AllRequiredNavigationProperties()
                                                                                   .Where(np => np.AssociationObject.SourceMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.One
                                                                                             || np.AssociationObject.TargetMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.One))
               {
                  NavigationProperty otherSide = requiredNavigationProperty.OtherSide;
                  string parameterName = requiredNavigationProperty.PropertyName.ToLower();
                  Output($"if ({parameterName} == null) throw new ArgumentNullException(nameof({parameterName}));");

                  if (!requiredNavigationProperty.ConstructorParameterOnly)
                  {
                     Output(requiredNavigationProperty.IsCollection
                               ? $"{requiredNavigationProperty.PropertyName}.Add({parameterName});"
                               : $"this.{requiredNavigationProperty.PropertyName} = {parameterName};");
                  }

                  if (!string.IsNullOrEmpty(otherSide.PropertyName))
                  {
                     Output(otherSide.IsCollection 
                               ? $"{parameterName}.{otherSide.PropertyName}.Add(this);" 
                               : $"{parameterName}.{otherSide.PropertyName} = this;");
                  }

                  NL();
               }

The addition of the if (!requiredNavigationProperty.ConstructorParameterOnly) condition is the fix.

Thanks for the report!

@Beast76
Copy link

Beast76 commented Feb 11, 2021

@pas059 and @Beast76 - since this is a code generation issue, which version of Entity Framework are you targeting?

I'm using the current latest version Microsoft.EntityFrameworkCore 5.0.3.

Thank you for the fast fix!

Kind regards

@pas059
Copy link
Author

pas059 commented Feb 11, 2021

hi,
thanks for the fix, this works!
best regards

msawczyn added a commit that referenced this issue Feb 11, 2021
   - Fix to detecting correct EF version with anything with "Latest" in it is configured (see #266)
   - Fix to generate correct initial value code for decimal properties (see #268)
   - Fix for constructor code generation in 1-N unidirectional associations (see #263)
@msawczyn msawczyn added released Issue is resolved in a current release and removed pending release Issue is resolved in the current codebase, will be published with the next release labels Feb 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Confirmed bug released Issue is resolved in a current release
Projects
None yet
Development

No branches or pull requests

3 participants