Complex Type requires primary key











up vote
0
down vote

favorite












I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.



public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}

[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}


While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).



The entity type Coordinate requires a primary key to be defined.



EDIT



For performance reasons I want the properties being stored as Coordinate_Latitude and Coordinate_Longitute instead of having a reference to another table.










share|improve this question




















  • 1




    exactly what I want to prevent, Why?
    – S.Akbari
    Nov 21 at 16:49












  • see edit - for performance reasons
    – David
    Nov 21 at 16:51






  • 1




    adding a primary (clustered) key will actually improve performance significantly
    – DaniDev
    Nov 21 at 17:54










  • I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
    – David
    Nov 21 at 22:03










  • I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
    – David
    Nov 21 at 22:07















up vote
0
down vote

favorite












I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.



public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}

[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}


While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).



The entity type Coordinate requires a primary key to be defined.



EDIT



For performance reasons I want the properties being stored as Coordinate_Latitude and Coordinate_Longitute instead of having a reference to another table.










share|improve this question




















  • 1




    exactly what I want to prevent, Why?
    – S.Akbari
    Nov 21 at 16:49












  • see edit - for performance reasons
    – David
    Nov 21 at 16:51






  • 1




    adding a primary (clustered) key will actually improve performance significantly
    – DaniDev
    Nov 21 at 17:54










  • I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
    – David
    Nov 21 at 22:03










  • I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
    – David
    Nov 21 at 22:07













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.



public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}

[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}


While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).



The entity type Coordinate requires a primary key to be defined.



EDIT



For performance reasons I want the properties being stored as Coordinate_Latitude and Coordinate_Longitute instead of having a reference to another table.










share|improve this question















I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.



public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}

[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}


While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).



The entity type Coordinate requires a primary key to be defined.



EDIT



For performance reasons I want the properties being stored as Coordinate_Latitude and Coordinate_Longitute instead of having a reference to another table.







c# .net entity-framework primary-key






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 18:35









S.Akbari

29.8k93471




29.8k93471










asked Nov 21 at 16:46









David

415




415








  • 1




    exactly what I want to prevent, Why?
    – S.Akbari
    Nov 21 at 16:49












  • see edit - for performance reasons
    – David
    Nov 21 at 16:51






  • 1




    adding a primary (clustered) key will actually improve performance significantly
    – DaniDev
    Nov 21 at 17:54










  • I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
    – David
    Nov 21 at 22:03










  • I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
    – David
    Nov 21 at 22:07














  • 1




    exactly what I want to prevent, Why?
    – S.Akbari
    Nov 21 at 16:49












  • see edit - for performance reasons
    – David
    Nov 21 at 16:51






  • 1




    adding a primary (clustered) key will actually improve performance significantly
    – DaniDev
    Nov 21 at 17:54










  • I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
    – David
    Nov 21 at 22:03










  • I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
    – David
    Nov 21 at 22:07








1




1




exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49






exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49














see edit - for performance reasons
– David
Nov 21 at 16:51




see edit - for performance reasons
– David
Nov 21 at 16:51




1




1




adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54




adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54












I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03




I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03












I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07




I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07












2 Answers
2






active

oldest

votes

















up vote
2
down vote













You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:



[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CoordinateKey { get; set; }


Or with Fluent-API:



protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}


Edit: It seems you want to treat the Coordinate class as a not mapped class. You can use NotMapped attribute. Have a look at the following question to know how:



Entity Framework code first: How to ignore classes






share|improve this answer






























    up vote
    0
    down vote













    Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.



    public class Location : IModule
    {
    public string Id { get; set; }
    public Coordinate Coordinate { get; set; }
    }

    [Owned]
    public class Coordinate
    {
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    }


    This creates a table containt the attributes Id, Coordinate_Latitued, Coordinate_Longitude.






    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',
      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%2f53416869%2fcomplex-type-requires-primary-key%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








      up vote
      2
      down vote













      You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:



      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int CoordinateKey { get; set; }


      Or with Fluent-API:



      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
      modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
      modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      }


      Edit: It seems you want to treat the Coordinate class as a not mapped class. You can use NotMapped attribute. Have a look at the following question to know how:



      Entity Framework code first: How to ignore classes






      share|improve this answer



























        up vote
        2
        down vote













        You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:



        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CoordinateKey { get; set; }


        Or with Fluent-API:



        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
        modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        }


        Edit: It seems you want to treat the Coordinate class as a not mapped class. You can use NotMapped attribute. Have a look at the following question to know how:



        Entity Framework code first: How to ignore classes






        share|improve this answer

























          up vote
          2
          down vote










          up vote
          2
          down vote









          You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:



          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int CoordinateKey { get; set; }


          Or with Fluent-API:



          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
          modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
          modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
          .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          }


          Edit: It seems you want to treat the Coordinate class as a not mapped class. You can use NotMapped attribute. Have a look at the following question to know how:



          Entity Framework code first: How to ignore classes






          share|improve this answer














          You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:



          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int CoordinateKey { get; set; }


          Or with Fluent-API:



          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
          modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
          modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
          .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          }


          Edit: It seems you want to treat the Coordinate class as a not mapped class. You can use NotMapped attribute. Have a look at the following question to know how:



          Entity Framework code first: How to ignore classes







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 7:42

























          answered Nov 21 at 16:52









          S.Akbari

          29.8k93471




          29.8k93471
























              up vote
              0
              down vote













              Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.



              public class Location : IModule
              {
              public string Id { get; set; }
              public Coordinate Coordinate { get; set; }
              }

              [Owned]
              public class Coordinate
              {
              public string Latitude { get; set; }
              public string Longitude { get; set; }
              }


              This creates a table containt the attributes Id, Coordinate_Latitued, Coordinate_Longitude.






              share|improve this answer

























                up vote
                0
                down vote













                Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.



                public class Location : IModule
                {
                public string Id { get; set; }
                public Coordinate Coordinate { get; set; }
                }

                [Owned]
                public class Coordinate
                {
                public string Latitude { get; set; }
                public string Longitude { get; set; }
                }


                This creates a table containt the attributes Id, Coordinate_Latitued, Coordinate_Longitude.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.



                  public class Location : IModule
                  {
                  public string Id { get; set; }
                  public Coordinate Coordinate { get; set; }
                  }

                  [Owned]
                  public class Coordinate
                  {
                  public string Latitude { get; set; }
                  public string Longitude { get; set; }
                  }


                  This creates a table containt the attributes Id, Coordinate_Latitued, Coordinate_Longitude.






                  share|improve this answer












                  Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.



                  public class Location : IModule
                  {
                  public string Id { get; set; }
                  public Coordinate Coordinate { get; set; }
                  }

                  [Owned]
                  public class Coordinate
                  {
                  public string Latitude { get; set; }
                  public string Longitude { get; set; }
                  }


                  This creates a table containt the attributes Id, Coordinate_Latitued, Coordinate_Longitude.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 12:30









                  David

                  415




                  415






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53416869%2fcomplex-type-requires-primary-key%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

                      Berounka

                      Fiat S.p.A.

                      Type 'String' is not a subtype of type 'int' of 'index'