英文:
Dynamically Set TEntity Name to DbContext.Set
问题
I can provide a translation of the code update section you provided:
Type LogType = Type.GetType("UserLogs");
if (LogType != null)
{
var LogObject = Activator.CreateInstance(LogType);
if (LogObject != null)
{
foreach (PropertyInfo property in LogType.GetProperties())
{
if (property.CanWrite)
{
if (property.PropertyType == typeof(string))
{
property.SetValue(LogObject, Convert.ToString(Entity.GetType().GetProperty(property.Name)?.GetValue(Entity)));
}
}
}
var TEntity = _dbContext.Model.GetEntityTypes().First(x => x.Name.Contains("UserLogs"));
await _dbContext.Set<UserLogs>().AddAsync((UserLogs)LogObject);
}
}
Please note that this code appears to be dynamically creating an object of type "UserLogs" and adding it to the _dbContext
using Entity Framework.
英文:
I am new to C# and Entity Framework. I have a scenario where I want to Insert Record to a Table based on Some Check and Conditions.
The problem is I do not know What Entity would be used to perform Insert/Update operation.
As you can see in Following C# Code I can Add UserLogs and PageLogs Entities in my Repository (Unit Of Work).
await _dbContext.Set<UserLogs>().AddAsync((UserLogs)ObjectChanges);
await _dbContext.Set<PageLogs>().AddAsync((PageLogs)ObjectChanges);
But in my Scenario, I only have String Names of Entities ("UserLogs", "PageLogs") and I can not Assign String Variable to _dbContext.Set<>
I tried to Get TEntityType by using
var TEntity = _dbContext.Model.GetEntityTypes()
.First(x => x.Name.Contains("UserLogs"));
I also have Tried to follow solution given to this Question.
But this also does not work, so I would like to know if there is any solution to this problem where I can DbSet a Dynamic Entity based on String Name.
Code Update
Type LogType = Type.GetType($"UserLogs");
if (LogType != null)
{
var LogObject = Activator.CreateInstance(LogType);
if (LogObject != null)
{
foreach (PropertyInfo property in LogType.GetProperties())
{
if (property.CanWrite)
{
if (property.PropertyType == typeof(string))
{
property.SetValue(LogObject, Convert.ToString(Entity.GetType().GetProperty(property.Name)?.GetValue(Entity)));
}
}
}
var TEntity = _dbContext.Model.GetEntityTypes().First(x => x.Name.Contains("UserLogs"));
await _dbContext.Set<$"UserLogs">().AddAsync(($"UserLogs")LogObject);
}
}
答案1
得分: 1
EF Core没有非泛型的DbSet
类。但它并不需要,因为DbContext
类提供了所有泛型DbSet<T>
方法的非泛型等效方法(除了查询)。
例如,Add(Object)
:
public virtual EntityEntry Add(object entity);
类似的还有AddAsync
、Update{Async}
、Remove{Async}
等方法。
它内部使用传递对象的GetType()
来确定实体类型(因此确定表)。因此,只要您能传递正确的对象实例(即使在您的原始问题中也必须这样做),就可以使用这些方法执行所需的操作。
例如,使用您的示例:
await _dbContext.AddAsync((object)ObjectChanges);
英文:
EF Core has no non generic DbSet
class. But it does not need to, since the DbContext
class provides non generic equivalents of all generic DbSet<T>
methods (except for querying).
For instance, Add(Object)
:
>cs
>public virtual EntityEntry Add(object entity);
>
Similar for AddAsync
, Update{Async}
, Remove{Async}
etc.
Internally it uses GetType()
of the passed object to determine the entity type (thus table). So as soon as you can pass correct object instances (you need to do that anyway even with your original question), you can use these methods to perform the requested action.
e.g. with your example
await _dbContext.AddAsync((object)ObjectChanges);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论