none
Is there have a way to dynamically add Dbset when using EF CORE 2.2? RRS feed

  • Question

  • I will ask about dynamically add Dbsets.

    The situation is, I want to dynamically add entity classes when service on. Just like

    namespace entity.db
    {
        [Table("some table name")]
        public class SomeClassName
        {
            public int Id { get; private set; }
            public DateTime Date { get; set; }
            public string Name { get; set; }
            public string Ext { get; set; }
            public string Email { get; set; }
        }
    }

    several classes like above, and maybe more in future ( not exactly know the count now ). I want to dynamically add them all to EF Core DbContext so I don't need to add them by myself one by one. How can I do?

    Tuesday, May 14, 2019 2:53 AM

Answers

  • How can I do in AnotherClass to create Dbset<> that the TEntity is a variable so that I can manageless to the DataDbContext for future classes adding?

    No, that I know about in EF Core and non Core,  and in general with.NET an Object Oriented language, one cannot create a DBSet object  dynamically during runtime. One cannot create a class, a concreate type, dynamically at runtime. C# is not an interpretative language like JavaScript. 

    The only dynamic objects that can be built at runtime are discussed in the link, along with anaymous types.

     https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types

    https://en.wikipedia.org/wiki/Interpreted_language

    • Marked as answer by KAMyAw Tuesday, May 14, 2019 9:30 AM
    Tuesday, May 14, 2019 8:28 AM

All replies

  • Hi KAMyAw,

    Do you mean that you want to create a table via "Code First"? If so, here I found some articles that you can refer to.

    Getting Started with EF Core on ASP.NET Core with a New database;

    Code First Approach In ASP.NET Core MVC With EF Core Migration;

    ASP.NET Core Web API with EF Core Code-First Approach.

    Regards,

    Kyle

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    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, May 14, 2019 5:17 AM
    Moderator
  • No, I just want to create Dbset<> dynamically.

    Not like the document written:

    public class DataDbContext : DbContext
    {
        Dbset<SomeClasses>() Classes {get; set;}
        /*some other code*/
    }

    But like this:

    public class DataDbContext : DbContext { /*some other code*/ } public class AnotherClass { /*Some code that dynamically create Dbset<TEntity which I can get necessary classes

    from Assembly Reflection>*/ }


    How can I do in AnotherClass to create Dbset<> that the TEntity is a variable so that I can manageless to the DataDbContext for future classes adding?

    Tuesday, May 14, 2019 6:21 AM
  • How can I do in AnotherClass to create Dbset<> that the TEntity is a variable so that I can manageless to the DataDbContext for future classes adding?

    No, that I know about in EF Core and non Core,  and in general with.NET an Object Oriented language, one cannot create a DBSet object  dynamically during runtime. One cannot create a class, a concreate type, dynamically at runtime. C# is not an interpretative language like JavaScript. 

    The only dynamic objects that can be built at runtime are discussed in the link, along with anaymous types.

     https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types

    https://en.wikipedia.org/wiki/Interpreted_language

    • Marked as answer by KAMyAw Tuesday, May 14, 2019 9:30 AM
    Tuesday, May 14, 2019 8:28 AM
  • I see, thanks a lot. Then I will create them by myself.
    Tuesday, May 14, 2019 9:30 AM
  • I see, thanks a lot. Then I will create them by myself.

    I looked into this more. You may be able to do something in building the model dynamically at runtime.

    https://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/

    You may find other examples by using Bing or Google.

    But what has been explained as far as dynamic object creation using .NET is another matter.  

    Tuesday, May 14, 2019 10:09 AM