none
fluent API and WithMany and HasOptional function RRS feed

  • Question

  • code taken from https://stackoverflow.com/a/4812413

    public class Contact
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? SpouseId { get; set; }                
    
        public Contact Spouse { get; set; }
    }
    
    public class Ctp5Context : DbContext
    {
        public DbSet<Contact> Contacts { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Contact>()
                        .HasOptional(c => c.Spouse)
                        .WithMany()
                        .HasForeignKey(c => c.SpouseId);
        }
    }

    tell me what is the use and meaning of HasOptional and WithMany function ?

    HasOptional means column may have null value ?

    WithMany means not clear ?

            modelBuilder.Entity<Contact>()
                        .HasOptional(c => c.Spouse)
                        .WithMany()
                        .HasForeignKey(c => c.SpouseId);
    Monday, July 24, 2017 12:00 PM

Answers

  • Hi Mou_kolkata,

    >>tell me what is the use and meaning of HasOptional and WithMany function ?

    According to your description and related code. it is a 0-1 to Many relationship with Spouse and Contact

    >>HasOptional means column may have null value ?

    yes.

    >>WithMany means not clear ?

    WithMany means that Spouse has many Contacts (0,1,2 ...);

    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.


    Tuesday, July 25, 2017 6:52 AM
    Moderator

All replies

  • Hi Mou_kolkata,

    >>tell me what is the use and meaning of HasOptional and WithMany function ?

    According to your description and related code. it is a 0-1 to Many relationship with Spouse and Contact

    >>HasOptional means column may have null value ?

    yes.

    >>WithMany means not clear ?

    WithMany means that Spouse has many Contacts (0,1,2 ...);

    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.


    Tuesday, July 25, 2017 6:52 AM
    Moderator
  • is this example is self referential because contact is declared as a type inside contact entity with a name Spouse ?

    public class Contact
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? SpouseId { get; set; }                
    
        public Contact Spouse { get; set; }
    }
    
    public class Ctp5Context : DbContext
    {
        public DbSet<Contact> Contacts { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Contact>()
                        .HasOptional(c => c.Spouse)
                        .WithMany()
                        .HasForeignKey(c => c.SpouseId);
        }
    }

    Tuesday, July 25, 2017 10:00 AM
  • Hi Mou_kolkata,

    >>is this example is self referential because contact is declared as a type inside contact entity with a name Spouse ?

    Yes.

    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.

    Wednesday, July 26, 2017 9:26 AM
    Moderator