Share via

Dynamic DbSet

Kiran Kumar 20 Reputation points
2023-06-24T17:06:41.4466667+00:00

Hi,

Can we add entity to DbSet dynamically like I have IEntityType but here I'm registering "object" as DbSet instead of object can I set "XXXXX" as DbSet dynamically.

  var newEntityType = dbContext.Model.GetEntityTypes().FirstOrDefault(t => t.Name == "XXXXX");
  dbContext.Set<object>();
  dbContext.Add(newEntity);
  int i= dbContext.SaveChanges();
Developer technologies | ASP.NET Core | Other
0 comments No comments

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2023-06-24T20:26:11.2333333+00:00

    The whole point of EF and dbset is to produce a strongly typed entries for tables and query responses.

    C# lacks true dynamic objects support, so if you need dynamic queries use ado datatables or use a dynamic language like JavaScript or python.

    note: the expando object support you asked about in your previous question is part of the .net dynamic runtime created to support iron ruby and iron python. Microsoft dropped both these products and the dynamic runtime is basically a second class library.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.