locked
Fluent API to create database in ef 7 code first RRS feed

  • Question

  • Say I want to create three tables.

    I want to create them in Fluent API. I have gotten the following.

     public class CustomerContacts
        {
            [Key]
            public long CustomerId { get; set; }
            public string Btn { get; set; }
        }

    And

     public class Restrictions
        {
            [Key]
            public short RestrictionId { get; set; }
            public string Description { get; set; }
            public string BlockCode { get; set; }
        }

    Then how to deal with the middle one CustomerRestrictions. Say I use entity framework core(or Entity Framework 7) code first. Technology changes too fast. Thanks for help.

    Monday, June 6, 2016 7:50 PM

Answers

  • Hi HZ.USA,

    According to your description, it seems that it's a Many-to-many relationships. Many-to-many relationships without an entity class to represent the join table are not yet supported in EF7. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships. like this:

    class MyContext : DbContext
        {
            public DbSet<CustomerContacts> CustomerContacts{ get; set; }
            public DbSet<Restrictions> Restrictions{ get; set; }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<CustomerRestrictions>()
                    .HasKey(t => new { t.CustomerId , t.RestrictionId });
    
                modelBuilder.Entity<CustomerRestrictions>()
                    .HasOne(pt => pt.CustomerContacts)
                    .WithMany(p => p.CustomerRestrictions)
                    .HasForeignKey(pt => pt.CustomerId );
    
                modelBuilder.Entity<CustomerRestrictions>()
                    .HasOne(pt => pt.Restrictions)
                    .WithMany(t => t.CustomerRestrictions)
                    .HasForeignKey(pt => pt.RestrictionId);
            }
        }
    
        public class CustomerContacts
        {
           [Key]
            public long CustomerId { get; set; }
            public string Btn { get; set; }
            public List<CustomerRestrictions> CustomerRestrictions{ get; set; }
        }
    
        public class Restrictions
        {
           [Key]
            public short RestrictionId { get; set; }
            public string Description { get; set; }
            public string BlockCode { get; set; }
            public List<CustomerRestrictions> CustomerRestrictions{ get; set; }
        }
    
        public class CustomerRestrictions
        {
            public int CustomerId { get; set; }
            public CustomerContacts CustomerContacts{ get; set; }
    
            public string RestrictionId { get; set; }
            public Restrictions Restrictions { get; set; }
        }
    

    For more information, please refer to:

    http://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 7, 2016 9:27 AM