Code First : Unable to create 1 to many relationship. Creates 0…1 to many instead












0















Customer



public class Customer
{
public Customer()
{
Addresses = new List<Address>();
Reviews = new List<Review>();
Products = new List<Product>();
}
[Key]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public Address DefaultAddress { get; set; }
public int DefaultAddressId { get; set; }
public List<Address> Addresses { get; set; }
public List<Review> Reviews { get; set; }
public List<Product> Products { get; set; }
}


Product



public class Product
{
public Product()
{
Reviews = new List < Review >();
}

public int Id { get; set; }
public Category Category { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Specification { get; set; }
public List<Review> Reviews { get; set; }
public List<Customer> Customers { get; set; }
}


Review



public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public string CustomerId { get; set; }
}


}



Generated model



enter image description here



I want the relationship between Review and Customer to 1 to many not 0..1 to many. Each review must belong to one customer. I don't understand how the relationship is mapped properly for Review - Product but not for the customer.










share|improve this question

























  • Maybe because the key for customer is a string, which is nullable? I would always use a non-nullable type for a key.

    – oerkelens
    Nov 22 '18 at 7:20











  • @oerkelens in this scenario customerId is the email since it's unique for each customer. It has to be a string.

    – Enzio
    Nov 22 '18 at 7:28











  • never, i repeat never use a string as the PK. it will end in tears! sure make a unique constraint for email but use a proper type like int, guid.

    – JohnB
    Nov 22 '18 at 7:46











  • The email has to be a string. The technical unique key does NEVER have to be a string. It's often considered bad practice to even assign a functional meaning to a primary key.

    – oerkelens
    Nov 22 '18 at 8:10











  • Changing id of customer table to int fixed it. Thanks :)

    – Enzio
    Nov 22 '18 at 8:57
















0















Customer



public class Customer
{
public Customer()
{
Addresses = new List<Address>();
Reviews = new List<Review>();
Products = new List<Product>();
}
[Key]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public Address DefaultAddress { get; set; }
public int DefaultAddressId { get; set; }
public List<Address> Addresses { get; set; }
public List<Review> Reviews { get; set; }
public List<Product> Products { get; set; }
}


Product



public class Product
{
public Product()
{
Reviews = new List < Review >();
}

public int Id { get; set; }
public Category Category { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Specification { get; set; }
public List<Review> Reviews { get; set; }
public List<Customer> Customers { get; set; }
}


Review



public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public string CustomerId { get; set; }
}


}



Generated model



enter image description here



I want the relationship between Review and Customer to 1 to many not 0..1 to many. Each review must belong to one customer. I don't understand how the relationship is mapped properly for Review - Product but not for the customer.










share|improve this question

























  • Maybe because the key for customer is a string, which is nullable? I would always use a non-nullable type for a key.

    – oerkelens
    Nov 22 '18 at 7:20











  • @oerkelens in this scenario customerId is the email since it's unique for each customer. It has to be a string.

    – Enzio
    Nov 22 '18 at 7:28











  • never, i repeat never use a string as the PK. it will end in tears! sure make a unique constraint for email but use a proper type like int, guid.

    – JohnB
    Nov 22 '18 at 7:46











  • The email has to be a string. The technical unique key does NEVER have to be a string. It's often considered bad practice to even assign a functional meaning to a primary key.

    – oerkelens
    Nov 22 '18 at 8:10











  • Changing id of customer table to int fixed it. Thanks :)

    – Enzio
    Nov 22 '18 at 8:57














0












0








0








Customer



public class Customer
{
public Customer()
{
Addresses = new List<Address>();
Reviews = new List<Review>();
Products = new List<Product>();
}
[Key]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public Address DefaultAddress { get; set; }
public int DefaultAddressId { get; set; }
public List<Address> Addresses { get; set; }
public List<Review> Reviews { get; set; }
public List<Product> Products { get; set; }
}


Product



public class Product
{
public Product()
{
Reviews = new List < Review >();
}

public int Id { get; set; }
public Category Category { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Specification { get; set; }
public List<Review> Reviews { get; set; }
public List<Customer> Customers { get; set; }
}


Review



public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public string CustomerId { get; set; }
}


}



Generated model



enter image description here



I want the relationship between Review and Customer to 1 to many not 0..1 to many. Each review must belong to one customer. I don't understand how the relationship is mapped properly for Review - Product but not for the customer.










share|improve this question
















Customer



public class Customer
{
public Customer()
{
Addresses = new List<Address>();
Reviews = new List<Review>();
Products = new List<Product>();
}
[Key]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public Address DefaultAddress { get; set; }
public int DefaultAddressId { get; set; }
public List<Address> Addresses { get; set; }
public List<Review> Reviews { get; set; }
public List<Product> Products { get; set; }
}


Product



public class Product
{
public Product()
{
Reviews = new List < Review >();
}

public int Id { get; set; }
public Category Category { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Specification { get; set; }
public List<Review> Reviews { get; set; }
public List<Customer> Customers { get; set; }
}


Review



public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public string CustomerId { get; set; }
}


}



Generated model



enter image description here



I want the relationship between Review and Customer to 1 to many not 0..1 to many. Each review must belong to one customer. I don't understand how the relationship is mapped properly for Review - Product but not for the customer.







c# .net entity-framework entity-framework-6 ef-code-first






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:57







Enzio

















asked Nov 22 '18 at 7:15









EnzioEnzio

145111




145111













  • Maybe because the key for customer is a string, which is nullable? I would always use a non-nullable type for a key.

    – oerkelens
    Nov 22 '18 at 7:20











  • @oerkelens in this scenario customerId is the email since it's unique for each customer. It has to be a string.

    – Enzio
    Nov 22 '18 at 7:28











  • never, i repeat never use a string as the PK. it will end in tears! sure make a unique constraint for email but use a proper type like int, guid.

    – JohnB
    Nov 22 '18 at 7:46











  • The email has to be a string. The technical unique key does NEVER have to be a string. It's often considered bad practice to even assign a functional meaning to a primary key.

    – oerkelens
    Nov 22 '18 at 8:10











  • Changing id of customer table to int fixed it. Thanks :)

    – Enzio
    Nov 22 '18 at 8:57



















  • Maybe because the key for customer is a string, which is nullable? I would always use a non-nullable type for a key.

    – oerkelens
    Nov 22 '18 at 7:20











  • @oerkelens in this scenario customerId is the email since it's unique for each customer. It has to be a string.

    – Enzio
    Nov 22 '18 at 7:28











  • never, i repeat never use a string as the PK. it will end in tears! sure make a unique constraint for email but use a proper type like int, guid.

    – JohnB
    Nov 22 '18 at 7:46











  • The email has to be a string. The technical unique key does NEVER have to be a string. It's often considered bad practice to even assign a functional meaning to a primary key.

    – oerkelens
    Nov 22 '18 at 8:10











  • Changing id of customer table to int fixed it. Thanks :)

    – Enzio
    Nov 22 '18 at 8:57

















Maybe because the key for customer is a string, which is nullable? I would always use a non-nullable type for a key.

– oerkelens
Nov 22 '18 at 7:20





Maybe because the key for customer is a string, which is nullable? I would always use a non-nullable type for a key.

– oerkelens
Nov 22 '18 at 7:20













@oerkelens in this scenario customerId is the email since it's unique for each customer. It has to be a string.

– Enzio
Nov 22 '18 at 7:28





@oerkelens in this scenario customerId is the email since it's unique for each customer. It has to be a string.

– Enzio
Nov 22 '18 at 7:28













never, i repeat never use a string as the PK. it will end in tears! sure make a unique constraint for email but use a proper type like int, guid.

– JohnB
Nov 22 '18 at 7:46





never, i repeat never use a string as the PK. it will end in tears! sure make a unique constraint for email but use a proper type like int, guid.

– JohnB
Nov 22 '18 at 7:46













The email has to be a string. The technical unique key does NEVER have to be a string. It's often considered bad practice to even assign a functional meaning to a primary key.

– oerkelens
Nov 22 '18 at 8:10





The email has to be a string. The technical unique key does NEVER have to be a string. It's often considered bad practice to even assign a functional meaning to a primary key.

– oerkelens
Nov 22 '18 at 8:10













Changing id of customer table to int fixed it. Thanks :)

– Enzio
Nov 22 '18 at 8:57





Changing id of customer table to int fixed it. Thanks :)

– Enzio
Nov 22 '18 at 8:57












1 Answer
1






active

oldest

votes


















0














i only used Customer and Review models since those are the once you wanted an answer.
Following is the your model classes should be.



public class Customer
{
public Customer()
{
Reviews = new HashSet<Review>();
}

[Key]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }

public ICollection<Review> Reviews { get; set; }

}

public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public string CustomerId { get; set; }

[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}


this way you can have customers Review collection and cast it in to a list if you want.use Include() method in your linq query to lazy load the customers review collection.
for an example:



var customer = dbContext.Customer.Include("Reviews").where(x => x.Email == "john@gmail.com").FirstOrDefault();





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%2f53425656%2fcode-first-unable-to-create-1-to-many-relationship-creates-0-1-to-many-inst%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    i only used Customer and Review models since those are the once you wanted an answer.
    Following is the your model classes should be.



    public class Customer
    {
    public Customer()
    {
    Reviews = new HashSet<Review>();
    }

    [Key]
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }

    public ICollection<Review> Reviews { get; set; }

    }

    public class Review
    {
    public int Id { get; set; }
    public string Text { get; set; }
    public int Stars { get; set; }
    [Required]
    public int ProductId { get; set; }
    [Required]
    public string CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public Customer Customer { get; set; }
    }


    this way you can have customers Review collection and cast it in to a list if you want.use Include() method in your linq query to lazy load the customers review collection.
    for an example:



    var customer = dbContext.Customer.Include("Reviews").where(x => x.Email == "john@gmail.com").FirstOrDefault();





    share|improve this answer




























      0














      i only used Customer and Review models since those are the once you wanted an answer.
      Following is the your model classes should be.



      public class Customer
      {
      public Customer()
      {
      Reviews = new HashSet<Review>();
      }

      [Key]
      public string Email { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Password { get; set; }

      public ICollection<Review> Reviews { get; set; }

      }

      public class Review
      {
      public int Id { get; set; }
      public string Text { get; set; }
      public int Stars { get; set; }
      [Required]
      public int ProductId { get; set; }
      [Required]
      public string CustomerId { get; set; }

      [ForeignKey("CustomerId")]
      public Customer Customer { get; set; }
      }


      this way you can have customers Review collection and cast it in to a list if you want.use Include() method in your linq query to lazy load the customers review collection.
      for an example:



      var customer = dbContext.Customer.Include("Reviews").where(x => x.Email == "john@gmail.com").FirstOrDefault();





      share|improve this answer


























        0












        0








        0







        i only used Customer and Review models since those are the once you wanted an answer.
        Following is the your model classes should be.



        public class Customer
        {
        public Customer()
        {
        Reviews = new HashSet<Review>();
        }

        [Key]
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Password { get; set; }

        public ICollection<Review> Reviews { get; set; }

        }

        public class Review
        {
        public int Id { get; set; }
        public string Text { get; set; }
        public int Stars { get; set; }
        [Required]
        public int ProductId { get; set; }
        [Required]
        public string CustomerId { get; set; }

        [ForeignKey("CustomerId")]
        public Customer Customer { get; set; }
        }


        this way you can have customers Review collection and cast it in to a list if you want.use Include() method in your linq query to lazy load the customers review collection.
        for an example:



        var customer = dbContext.Customer.Include("Reviews").where(x => x.Email == "john@gmail.com").FirstOrDefault();





        share|improve this answer













        i only used Customer and Review models since those are the once you wanted an answer.
        Following is the your model classes should be.



        public class Customer
        {
        public Customer()
        {
        Reviews = new HashSet<Review>();
        }

        [Key]
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Password { get; set; }

        public ICollection<Review> Reviews { get; set; }

        }

        public class Review
        {
        public int Id { get; set; }
        public string Text { get; set; }
        public int Stars { get; set; }
        [Required]
        public int ProductId { get; set; }
        [Required]
        public string CustomerId { get; set; }

        [ForeignKey("CustomerId")]
        public Customer Customer { get; set; }
        }


        this way you can have customers Review collection and cast it in to a list if you want.use Include() method in your linq query to lazy load the customers review collection.
        for an example:



        var customer = dbContext.Customer.Include("Reviews").where(x => x.Email == "john@gmail.com").FirstOrDefault();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:13









        Chathura EdirisingheChathura Edirisinghe

        7114




        7114
































            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%2f53425656%2fcode-first-unable-to-create-1-to-many-relationship-creates-0-1-to-many-inst%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?