DEV Community

Cover image for EF Builder Entity One to Many Configuration
John Peters
John Peters

Posted on • Edited on

EF Builder Entity One to Many Configuration

            builder.Entity<Setting>()
             .HasOne(e => e.SettingGroup)
             .WithMany(fe => fe.Settings);
            builder.Entity<Setting>()
              .HasMany(p => p.Options)
              .WithOne()
              .HasForeignKey(p => p.SettingID);
            builder.Entity<Setting>()
              .HasMany(p => p.CurrentValueList)
              .WithOne().HasForeignKey(p => p.SettingID);
            builder.Entity<SettingOption>();
            builder.Entity<SettingValue>();
            builder.Entity<SettingsGroup>();
Enter fullscreen mode Exit fullscreen mode

EF is not without a bit of extra work, as we must (when using code-first) configure all the DB Relationships.

We have a class named Setting which looks like this:

    public class Setting
    {



        public int ID { get; set; }
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public string Owner { get; set; }
        public string Size { get; set; }
        public string Type { get; set; } 
        public int SettingGroupID { get; set; }
        public virtual SettingsGroup SettingGroup { get; set; }
        public virtual List<SettingOption> Options { get; set; } = new List<SettingOption>();
        public virtual List<SettingValue> CurrentValueList { get; set; } = new List<SettingValue>();
    }
}
Enter fullscreen mode Exit fullscreen mode

The first EF rule is that all the default properties will be automatically generated at Update Database time based on a Migration (No special configurations are needed for them). Migrations are a way for all the changes to be cataloged. If needed, changes can be rolled back (however we don't use that feature as we aren't in production yet).

Virtual Properties

All Virtual Properties indicate what is known as a navigation property; therefore, this line of code:

 builder.Entity<Setting>()
             .HasOne(e => e.SettingGroup)
             .WithMany(fe => fe.Settings);
Enter fullscreen mode Exit fullscreen mode

Is telling EF that the database relationship with the class "Setting" Has one "SettingGroup" which contains multiple "Settings". Indeed the class SettingsGroup has mulitple settings in it as shown below.

    public class SettingsGroup
    {
        public int ID { get; set; }
        public string UserName {get;set;}
        public string DisplayName { get; set; }
        public virtual List<Setting> Settings { get; set; }      
    }
Enter fullscreen mode Exit fullscreen mode

Notice that the "Settings" property above is virtual. This is a requirement for anything such as a list, or anything needing a Foreign Key relationship.

Once the database update is run, we see this in the "Settings" field named "SettingsControls".

Alt Text

Notice the PK and FK designations within the parenthesis.

This shows how to get your database's impedance mismatch synced with class objects within your project. Be patient when first learning this because it is a bit odd and confusing... Just keep plugging away and ask for help if you need it.

JWP2020

Top comments (0)