msawczyn/EFDesigner

1 to many unidirectionnal association

pas059 opened this issue · 5 comments

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

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

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

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!

@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

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