Multiplicity is not valid in Role 'CW_FirmaCommunication_CwFirma_Source' in relationship 'CW_FirmaCommunication_CwFirma'



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have the following problem:



I have a company (CW_Firma) which can have 0 to many ways to contact them (CW_FirmaCommunication).
I am using EntityFramework and FluentAPI to create the relationship.



I have the following code :



[Table("dbo.CW_Firma")]
public class CW_Firma

[Key]
[Column("F_VAT")]
public int VatNumber get; set;


[Table("dbo.CW_FirmaCommunication")]
public class CW_FirmaCommunication

[Key]
[Column("FC_VAT")]
public int VatNumber get; set;
[Column("FC_Data")]
public string FC_Data get; set;


protected override void OnModelCreating(DbModelBuilder modelBuilder)

modelBuilder.Entity<CW_FirmaCommunication>()
.HasRequired(c => c.CwFirma)
.WithMany()
.HasForeignKey(f => f.VatNumber);



When I run my code, I get the following exception :




Multiplicity is not valid in Role 'CW_FirmaCommunication_CwFirma_Source' in relationship 'CW_FirmaCommunication_CwFirma'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'




I have been searching the internet, looking at different solutions here on StackOverflow, but nothing seems to resolve my problem.



Can anyone tell me where I'm going wrong?










share|improve this question




























    0















    I have the following problem:



    I have a company (CW_Firma) which can have 0 to many ways to contact them (CW_FirmaCommunication).
    I am using EntityFramework and FluentAPI to create the relationship.



    I have the following code :



    [Table("dbo.CW_Firma")]
    public class CW_Firma

    [Key]
    [Column("F_VAT")]
    public int VatNumber get; set;


    [Table("dbo.CW_FirmaCommunication")]
    public class CW_FirmaCommunication

    [Key]
    [Column("FC_VAT")]
    public int VatNumber get; set;
    [Column("FC_Data")]
    public string FC_Data get; set;


    protected override void OnModelCreating(DbModelBuilder modelBuilder)

    modelBuilder.Entity<CW_FirmaCommunication>()
    .HasRequired(c => c.CwFirma)
    .WithMany()
    .HasForeignKey(f => f.VatNumber);



    When I run my code, I get the following exception :




    Multiplicity is not valid in Role 'CW_FirmaCommunication_CwFirma_Source' in relationship 'CW_FirmaCommunication_CwFirma'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'




    I have been searching the internet, looking at different solutions here on StackOverflow, but nothing seems to resolve my problem.



    Can anyone tell me where I'm going wrong?










    share|improve this question
























      0












      0








      0








      I have the following problem:



      I have a company (CW_Firma) which can have 0 to many ways to contact them (CW_FirmaCommunication).
      I am using EntityFramework and FluentAPI to create the relationship.



      I have the following code :



      [Table("dbo.CW_Firma")]
      public class CW_Firma

      [Key]
      [Column("F_VAT")]
      public int VatNumber get; set;


      [Table("dbo.CW_FirmaCommunication")]
      public class CW_FirmaCommunication

      [Key]
      [Column("FC_VAT")]
      public int VatNumber get; set;
      [Column("FC_Data")]
      public string FC_Data get; set;


      protected override void OnModelCreating(DbModelBuilder modelBuilder)

      modelBuilder.Entity<CW_FirmaCommunication>()
      .HasRequired(c => c.CwFirma)
      .WithMany()
      .HasForeignKey(f => f.VatNumber);



      When I run my code, I get the following exception :




      Multiplicity is not valid in Role 'CW_FirmaCommunication_CwFirma_Source' in relationship 'CW_FirmaCommunication_CwFirma'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'




      I have been searching the internet, looking at different solutions here on StackOverflow, but nothing seems to resolve my problem.



      Can anyone tell me where I'm going wrong?










      share|improve this question














      I have the following problem:



      I have a company (CW_Firma) which can have 0 to many ways to contact them (CW_FirmaCommunication).
      I am using EntityFramework and FluentAPI to create the relationship.



      I have the following code :



      [Table("dbo.CW_Firma")]
      public class CW_Firma

      [Key]
      [Column("F_VAT")]
      public int VatNumber get; set;


      [Table("dbo.CW_FirmaCommunication")]
      public class CW_FirmaCommunication

      [Key]
      [Column("FC_VAT")]
      public int VatNumber get; set;
      [Column("FC_Data")]
      public string FC_Data get; set;


      protected override void OnModelCreating(DbModelBuilder modelBuilder)

      modelBuilder.Entity<CW_FirmaCommunication>()
      .HasRequired(c => c.CwFirma)
      .WithMany()
      .HasForeignKey(f => f.VatNumber);



      When I run my code, I get the following exception :




      Multiplicity is not valid in Role 'CW_FirmaCommunication_CwFirma_Source' in relationship 'CW_FirmaCommunication_CwFirma'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'




      I have been searching the internet, looking at different solutions here on StackOverflow, but nothing seems to resolve my problem.



      Can anyone tell me where I'm going wrong?







      c# entity-framework-6 one-to-many ef-fluent-api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 14:39









      Bart SchelkensBart Schelkens

      41831029




      41831029






















          2 Answers
          2






          active

          oldest

          votes


















          0














          1. you use fluent api and mapping annotations - I would prefer to use only one,

          2. in annotation mapping you declared that field VatNumber of CW_FirmaCommunication table is primary key (as VatNumber in CW_Firma), which means that you have relation 1:1 while using fluent api you want 1:many.

          Basing on your code I made few corrects:



          [Table("dbo.CW_FirmaCommunication")]
          public class CW_FirmaCommunication

          [Key]
          [Column("Communnication_Id")]
          public int CommunicationId get; set;

          [Column("FC_VAT")]
          public int VatNumber get; set;

          public CW_Firma Firma get; set;

          [Column("FC_Data")]
          public string FC_Data get; set;



          I have added seperate PK for communications table (not FC_VAT), and added navigation property Firma.



          New mapping:



          modelBuilder.Entity<CW_FirmaCommunication>()
          .HasRequired<CW_Firma>(c => c.Firma)
          .WithMany()
          .HasForeignKey(f => f.VatNumber);


          It's working in my test db.






          share|improve this answer

























          • Thanks for your remarks, but how can I make it so my problem is solved? I tried removing the "Key" from CW_FirmaCommunication" but I still get the same error.

            – Bart Schelkens
            Nov 19 '18 at 7:07











          • I've changed my code to what you wrote, but now I'm getting : A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

            – Bart Schelkens
            Nov 19 '18 at 14:31











          • @BartSchelkens do you mean you have created tables in db and that new error is during quering data? In my tests tables had created. Maybe clear both tables and create them from start.

            – Grzesiek Danowski
            Nov 19 '18 at 15:04











          • I'm using an existing database. So I didn't create the tables, they were already there.

            – Bart Schelkens
            Nov 19 '18 at 15:07











          • Is FC_VAT unique in CW_Firma table?

            – Grzesiek Danowski
            Nov 19 '18 at 15:10


















          0














          After a lot of searching and trying, this is what I ended up with and what works fine:



          public class CW_FirmaCommunication

          [Key]
          public int FC_ID get; set;
          public int FC_VAT get; set;
          public int FC_Type get; set;
          public string FC_Data get; set;

          [ForeignKey("FC_VAT")]
          public virtual CW_Firma CwFirma get; set;


          public class CW_Firma

          [Key]
          public int F_VAT get; set;



          And I removed the code from the "OnModelCreating".






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339961%2fmultiplicity-is-not-valid-in-role-cw-firmacommunication-cwfirma-source-in-rela%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            1. you use fluent api and mapping annotations - I would prefer to use only one,

            2. in annotation mapping you declared that field VatNumber of CW_FirmaCommunication table is primary key (as VatNumber in CW_Firma), which means that you have relation 1:1 while using fluent api you want 1:many.

            Basing on your code I made few corrects:



            [Table("dbo.CW_FirmaCommunication")]
            public class CW_FirmaCommunication

            [Key]
            [Column("Communnication_Id")]
            public int CommunicationId get; set;

            [Column("FC_VAT")]
            public int VatNumber get; set;

            public CW_Firma Firma get; set;

            [Column("FC_Data")]
            public string FC_Data get; set;



            I have added seperate PK for communications table (not FC_VAT), and added navigation property Firma.



            New mapping:



            modelBuilder.Entity<CW_FirmaCommunication>()
            .HasRequired<CW_Firma>(c => c.Firma)
            .WithMany()
            .HasForeignKey(f => f.VatNumber);


            It's working in my test db.






            share|improve this answer

























            • Thanks for your remarks, but how can I make it so my problem is solved? I tried removing the "Key" from CW_FirmaCommunication" but I still get the same error.

              – Bart Schelkens
              Nov 19 '18 at 7:07











            • I've changed my code to what you wrote, but now I'm getting : A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

              – Bart Schelkens
              Nov 19 '18 at 14:31











            • @BartSchelkens do you mean you have created tables in db and that new error is during quering data? In my tests tables had created. Maybe clear both tables and create them from start.

              – Grzesiek Danowski
              Nov 19 '18 at 15:04











            • I'm using an existing database. So I didn't create the tables, they were already there.

              – Bart Schelkens
              Nov 19 '18 at 15:07











            • Is FC_VAT unique in CW_Firma table?

              – Grzesiek Danowski
              Nov 19 '18 at 15:10















            0














            1. you use fluent api and mapping annotations - I would prefer to use only one,

            2. in annotation mapping you declared that field VatNumber of CW_FirmaCommunication table is primary key (as VatNumber in CW_Firma), which means that you have relation 1:1 while using fluent api you want 1:many.

            Basing on your code I made few corrects:



            [Table("dbo.CW_FirmaCommunication")]
            public class CW_FirmaCommunication

            [Key]
            [Column("Communnication_Id")]
            public int CommunicationId get; set;

            [Column("FC_VAT")]
            public int VatNumber get; set;

            public CW_Firma Firma get; set;

            [Column("FC_Data")]
            public string FC_Data get; set;



            I have added seperate PK for communications table (not FC_VAT), and added navigation property Firma.



            New mapping:



            modelBuilder.Entity<CW_FirmaCommunication>()
            .HasRequired<CW_Firma>(c => c.Firma)
            .WithMany()
            .HasForeignKey(f => f.VatNumber);


            It's working in my test db.






            share|improve this answer

























            • Thanks for your remarks, but how can I make it so my problem is solved? I tried removing the "Key" from CW_FirmaCommunication" but I still get the same error.

              – Bart Schelkens
              Nov 19 '18 at 7:07











            • I've changed my code to what you wrote, but now I'm getting : A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

              – Bart Schelkens
              Nov 19 '18 at 14:31











            • @BartSchelkens do you mean you have created tables in db and that new error is during quering data? In my tests tables had created. Maybe clear both tables and create them from start.

              – Grzesiek Danowski
              Nov 19 '18 at 15:04











            • I'm using an existing database. So I didn't create the tables, they were already there.

              – Bart Schelkens
              Nov 19 '18 at 15:07











            • Is FC_VAT unique in CW_Firma table?

              – Grzesiek Danowski
              Nov 19 '18 at 15:10













            0












            0








            0







            1. you use fluent api and mapping annotations - I would prefer to use only one,

            2. in annotation mapping you declared that field VatNumber of CW_FirmaCommunication table is primary key (as VatNumber in CW_Firma), which means that you have relation 1:1 while using fluent api you want 1:many.

            Basing on your code I made few corrects:



            [Table("dbo.CW_FirmaCommunication")]
            public class CW_FirmaCommunication

            [Key]
            [Column("Communnication_Id")]
            public int CommunicationId get; set;

            [Column("FC_VAT")]
            public int VatNumber get; set;

            public CW_Firma Firma get; set;

            [Column("FC_Data")]
            public string FC_Data get; set;



            I have added seperate PK for communications table (not FC_VAT), and added navigation property Firma.



            New mapping:



            modelBuilder.Entity<CW_FirmaCommunication>()
            .HasRequired<CW_Firma>(c => c.Firma)
            .WithMany()
            .HasForeignKey(f => f.VatNumber);


            It's working in my test db.






            share|improve this answer















            1. you use fluent api and mapping annotations - I would prefer to use only one,

            2. in annotation mapping you declared that field VatNumber of CW_FirmaCommunication table is primary key (as VatNumber in CW_Firma), which means that you have relation 1:1 while using fluent api you want 1:many.

            Basing on your code I made few corrects:



            [Table("dbo.CW_FirmaCommunication")]
            public class CW_FirmaCommunication

            [Key]
            [Column("Communnication_Id")]
            public int CommunicationId get; set;

            [Column("FC_VAT")]
            public int VatNumber get; set;

            public CW_Firma Firma get; set;

            [Column("FC_Data")]
            public string FC_Data get; set;



            I have added seperate PK for communications table (not FC_VAT), and added navigation property Firma.



            New mapping:



            modelBuilder.Entity<CW_FirmaCommunication>()
            .HasRequired<CW_Firma>(c => c.Firma)
            .WithMany()
            .HasForeignKey(f => f.VatNumber);


            It's working in my test db.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 10:36

























            answered Nov 16 '18 at 16:34









            Grzesiek DanowskiGrzesiek Danowski

            21217




            21217












            • Thanks for your remarks, but how can I make it so my problem is solved? I tried removing the "Key" from CW_FirmaCommunication" but I still get the same error.

              – Bart Schelkens
              Nov 19 '18 at 7:07











            • I've changed my code to what you wrote, but now I'm getting : A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

              – Bart Schelkens
              Nov 19 '18 at 14:31











            • @BartSchelkens do you mean you have created tables in db and that new error is during quering data? In my tests tables had created. Maybe clear both tables and create them from start.

              – Grzesiek Danowski
              Nov 19 '18 at 15:04











            • I'm using an existing database. So I didn't create the tables, they were already there.

              – Bart Schelkens
              Nov 19 '18 at 15:07











            • Is FC_VAT unique in CW_Firma table?

              – Grzesiek Danowski
              Nov 19 '18 at 15:10

















            • Thanks for your remarks, but how can I make it so my problem is solved? I tried removing the "Key" from CW_FirmaCommunication" but I still get the same error.

              – Bart Schelkens
              Nov 19 '18 at 7:07











            • I've changed my code to what you wrote, but now I'm getting : A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

              – Bart Schelkens
              Nov 19 '18 at 14:31











            • @BartSchelkens do you mean you have created tables in db and that new error is during quering data? In my tests tables had created. Maybe clear both tables and create them from start.

              – Grzesiek Danowski
              Nov 19 '18 at 15:04











            • I'm using an existing database. So I didn't create the tables, they were already there.

              – Bart Schelkens
              Nov 19 '18 at 15:07











            • Is FC_VAT unique in CW_Firma table?

              – Grzesiek Danowski
              Nov 19 '18 at 15:10
















            Thanks for your remarks, but how can I make it so my problem is solved? I tried removing the "Key" from CW_FirmaCommunication" but I still get the same error.

            – Bart Schelkens
            Nov 19 '18 at 7:07





            Thanks for your remarks, but how can I make it so my problem is solved? I tried removing the "Key" from CW_FirmaCommunication" but I still get the same error.

            – Bart Schelkens
            Nov 19 '18 at 7:07













            I've changed my code to what you wrote, but now I'm getting : A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

            – Bart Schelkens
            Nov 19 '18 at 14:31





            I've changed my code to what you wrote, but now I'm getting : A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

            – Bart Schelkens
            Nov 19 '18 at 14:31













            @BartSchelkens do you mean you have created tables in db and that new error is during quering data? In my tests tables had created. Maybe clear both tables and create them from start.

            – Grzesiek Danowski
            Nov 19 '18 at 15:04





            @BartSchelkens do you mean you have created tables in db and that new error is during quering data? In my tests tables had created. Maybe clear both tables and create them from start.

            – Grzesiek Danowski
            Nov 19 '18 at 15:04













            I'm using an existing database. So I didn't create the tables, they were already there.

            – Bart Schelkens
            Nov 19 '18 at 15:07





            I'm using an existing database. So I didn't create the tables, they were already there.

            – Bart Schelkens
            Nov 19 '18 at 15:07













            Is FC_VAT unique in CW_Firma table?

            – Grzesiek Danowski
            Nov 19 '18 at 15:10





            Is FC_VAT unique in CW_Firma table?

            – Grzesiek Danowski
            Nov 19 '18 at 15:10













            0














            After a lot of searching and trying, this is what I ended up with and what works fine:



            public class CW_FirmaCommunication

            [Key]
            public int FC_ID get; set;
            public int FC_VAT get; set;
            public int FC_Type get; set;
            public string FC_Data get; set;

            [ForeignKey("FC_VAT")]
            public virtual CW_Firma CwFirma get; set;


            public class CW_Firma

            [Key]
            public int F_VAT get; set;



            And I removed the code from the "OnModelCreating".






            share|improve this answer



























              0














              After a lot of searching and trying, this is what I ended up with and what works fine:



              public class CW_FirmaCommunication

              [Key]
              public int FC_ID get; set;
              public int FC_VAT get; set;
              public int FC_Type get; set;
              public string FC_Data get; set;

              [ForeignKey("FC_VAT")]
              public virtual CW_Firma CwFirma get; set;


              public class CW_Firma

              [Key]
              public int F_VAT get; set;



              And I removed the code from the "OnModelCreating".






              share|improve this answer

























                0












                0








                0







                After a lot of searching and trying, this is what I ended up with and what works fine:



                public class CW_FirmaCommunication

                [Key]
                public int FC_ID get; set;
                public int FC_VAT get; set;
                public int FC_Type get; set;
                public string FC_Data get; set;

                [ForeignKey("FC_VAT")]
                public virtual CW_Firma CwFirma get; set;


                public class CW_Firma

                [Key]
                public int F_VAT get; set;



                And I removed the code from the "OnModelCreating".






                share|improve this answer













                After a lot of searching and trying, this is what I ended up with and what works fine:



                public class CW_FirmaCommunication

                [Key]
                public int FC_ID get; set;
                public int FC_VAT get; set;
                public int FC_Type get; set;
                public string FC_Data get; set;

                [ForeignKey("FC_VAT")]
                public virtual CW_Firma CwFirma get; set;


                public class CW_Firma

                [Key]
                public int F_VAT get; set;



                And I removed the code from the "OnModelCreating".







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 10:05









                Bart SchelkensBart Schelkens

                41831029




                41831029



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339961%2fmultiplicity-is-not-valid-in-role-cw-firmacommunication-cwfirma-source-in-rela%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    Evgeni Malkin