Dynamically Adding DbSet Properties in DbContext for Entity Framework Code First

When working with Entity Framework Code First, the general pattern to add DbSet properties to the DbContext object is as follows. The Entity Framework will build the model according to the DbSet properties defined in the DbContext.

public class MyAppContext : DbContext

    {

        DbSet<Customer> customers { get; set; }

    }

However, for a project with even a fair bit of domain complexity, defining DbSet properties for all entities would not be possible and we need a way to dynamically read our Entity Types and add them dynamically in the DbContext. This can be achieved by implementing OnModelCreating method of the DbContext. The Entity Types can be read from either the executing assembly Assembly.GetExecutingAssembly() or by loading more than one assemblies which can be placed in a configuration file (as shown below in the example). For details about setting up the custom configuration section with your assemblies see my last post

The following code will add all Class Types to the DbContext for all the assemblies added in the configuration file, notice we are actually calling modelBuilder.Entity<T>() method through reflection.

 

public class MyAppContext : DbContext

    {

        protected override void OnModelCreating(DbModelBuilder modelBuilder)

        {

            CustomAssemblySection configSection = (CustomAssemblySection)System.Configuration.ConfigurationManager.GetSection("CustomAssemblySection");

 

            foreach (CustomAssembly customAssembly in configSection.Assemblies)

            {

                Assembly assembly = Assembly.Load(customAssembly.Name);

                foreach (Type type in assembly.ExportedTypes)

                {

                    if (type.IsClass)

                    {

                        MethodInfo method = modelBuilder.GetType().GetMethod("Entity");

                        method = method.MakeGenericMethod(new Type[] { type });

                        method.Invoke(modelBuilder, null);

                    }

                }

            }

            base.OnModelCreating(modelBuilder);

        }

    }

Advertisements

Adding Custom Configuration Section in .NET Configuration Files

In order to add a Custom Configuration Section in any of .NET configuration files like app.config or web.cofig, the framework provides classes in the System.Configuration namespace such as System.Configuration.ConfigurationSection, System.Configuration.ConfigurationElement, System.Configuration.ConfigurationElementCollection and others.

Lets say we want to add the following Custom Section in the configuration file which can hold a list of assemblies.

<CustomAssemblySection>

    <assemblies>

      <add name="System.Configuration"></add>

      <add name="System.Data.Entity"></add>

      <add name="System.Data"></add>

      <add name="System.Xml"></add>

      <add name="System.Xml.Linq"></add>

      <add name="System.Core"></add>

    </assemblies>

  </CustomAssemblySection>

We need a Custom ConfigurationSection, a Custom ConfigurationElementCollection (assemblies) and a Cusom ConfigurationElement with one property which is “name” of the assembly. The following code is achieving exactly that. We have three classes CustomAssemblySection, CustomAssemblyCollection and CustomAssembly which inherit from the respective classes of System.Configuration.

Following is code for a simple Console Application with Program.cs and App.config files. The Program Main is reading the configuration and writing the assembly names on the Console.

For further reading you can refer to MSDN How To: http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Configuration;

namespace CustomConfigurationSectionConsole

{

    class Program

    {

        static void Main(string[] args)

        {

            CustomAssemblySection customSection = (CustomAssemblySection)System.Configuration.ConfigurationManager.GetSection("CustomAssemblySection");

             foreach (CustomAssemblyElement customAssembly in customSection.Assemblies)

             {

                 Console.WriteLine(customAssembly.Name);

             }

             Console.ReadLine();

        }

    }

    public class CustomAssemblySection : ConfigurationSection

    {

        [ConfigurationProperty("assemblies", IsDefaultCollection = false)]

        [ConfigurationCollection(typeof(CustomAssemblyCollection))]

        public CustomAssemblyCollection Assemblies

        {

            get

            {

                return (CustomAssemblyCollection)base["assemblies"];

            }

        }

    }

    public class CustomAssemblyCollection : ConfigurationElementCollection

    {

        protected override ConfigurationElement CreateNewElement()

        {

            return new CustomAssemblyElement();

        }

        protected override object GetElementKey(ConfigurationElement element)

        {

            return ((CustomAssemblyElement)element).Name;

        }

        public CustomAssemblyElement this[int Index]

        {

            get

            {

                return (CustomAssemblyElement)BaseGet(Index);

            }

            set

            {

                if (BaseGet(Index) != null)

                {

                    BaseRemoveAt(Index);

                }

                BaseAdd(Index, value);

            }

        }

        new public CustomAssemblyElement this[string Name]

        {

            get

            {

                return (CustomAssemblyElement)BaseGet(Name);

            }

        }

    }

    public class CustomAssemblyElement : ConfigurationElement

    {

        [ConfigurationProperty("name", IsRequired = true)]

        public string Name

        {

            get

            {

                return (string)this["name"];

            }

            set

            {

                this["name"] = value;

            }

        }

    }

}

App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="CustomAssemblySection" type="CustomConfigurationSectionConsole.CustomAssemblySection, CustomConfigurationSectionConsole"/>

  </configSections>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

    </startup>

  <CustomAssemblySection>

    <assemblies>

      <add name="System.Configuration"></add>

      <add name="System.Data.Entity"></add>

      <add name="System.Data"></add>

      <add name="System.Xml"></add>

      <add name="System.Xml.Linq"></add>

      <add name="System.Core"></add>

    </assemblies>

  </CustomAssemblySection>

</configuration>