none
Exception, Multiplicity constraint violated RRS feed

  • Question


  • I have uploaded a simplified example to OneDrive.

    https://1drv.ms/u/s!AnefnfzQ-qsZhBVnJkwtwUG5LnL9

    The example is a One-To-One-To-Many relationship. I cannot get EF to create the One-To-One correctly.  On clicking the query button, the following error occurs:

    Multiplicity constraint violated. The role 'ModuleStatus_Module_Source' of the relationship 'Enterprise.Dsa.Common.Database.ModuleStatus_Module' has multiplicity 1 or 0..1.

    The example is a winform app.  Just edit the EntityFrameworkTestForm to add DB connection properties.

    I have added the Key and ForeignKey attributes, navigation properties, etc.  Nothing seems to work.  Creating the example seemed the easiest way to demonstrate the problem.

    Hopefully someone can help with this,

    Thank you



    • Edited by Jeff13579 Tuesday, February 14, 2017 12:35 AM A little more example detail
    Tuesday, February 14, 2017 12:07 AM

Answers

  • Hi Jeff13579,

    I download the project and reproduce your issue on my side.

    from the Module constructor. Instantiating navigation references is a source for known problems (in contrast to instantiating empty navigation collections which is fine). It is the reason why you get a ModuleStatus with default values.

    Please remove ModuleStatus = new ModuleStatus(); in your Module Class. like this:

    public class Module
        {
            [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
            public Guid ModuleId { get; set; }
    
            //[InverseProperty("Modules")]
            public virtual ModuleStatus ModuleStatus { get; set; }
    
            /// <summary>
            /// UI displayed name
            /// </summary>
            public string Name { get; set; }
    
            //public Module()
            //{
            //    ModuleStatus = new ModuleStatus();
            //}
    
        }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff13579 Tuesday, February 14, 2017 2:02 PM
    Tuesday, February 14, 2017 5:58 AM
    Moderator

All replies

  • Hi Jeff13579,

    I download the project and reproduce your issue on my side.

    from the Module constructor. Instantiating navigation references is a source for known problems (in contrast to instantiating empty navigation collections which is fine). It is the reason why you get a ModuleStatus with default values.

    Please remove ModuleStatus = new ModuleStatus(); in your Module Class. like this:

    public class Module
        {
            [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
            public Guid ModuleId { get; set; }
    
            //[InverseProperty("Modules")]
            public virtual ModuleStatus ModuleStatus { get; set; }
    
            /// <summary>
            /// UI displayed name
            /// </summary>
            public string Name { get; set; }
    
            //public Module()
            //{
            //    ModuleStatus = new ModuleStatus();
            //}
    
        }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff13579 Tuesday, February 14, 2017 2:02 PM
    Tuesday, February 14, 2017 5:58 AM
    Moderator
  • Well that was simple.  Thank you,

    I have a habit of initializing objects in the constructor to avoid null references, I guess EF will take care of that for me since the property is required or initialize only on use.


    • Marked as answer by Jeff13579 Tuesday, February 14, 2017 2:02 PM
    • Unmarked as answer by Jeff13579 Tuesday, February 14, 2017 2:02 PM
    • Edited by Jeff13579 Tuesday, February 14, 2017 2:11 PM
    Tuesday, February 14, 2017 2:01 PM