为什么我的 Entity Framework 模型中不能有 属性 类型的类型?

Why can't I have property of type Type in my model for Entity Framework?

我正在为一个项目设置模型,除以下更改外,一切都按预期工作。我认为将类型指定为 Type 而不是将其描述为 string.

会很好
namespace DataBase.Entities
{
  public class Lock
  {
    public Guid Id { get; set; }
    public DateTime? Occasion { get; set; }
    public int Counter { get; set; }
    public Type Entity { get; set; }
    //public string Entity { get; set; }
  }
}

你猜怎么着! EF一点也不喜欢它。然后添加显式迁移时出现的错误如下,我不知道为什么。

System.ArgumentNullException: Value cannot be null.
Parameter name: entitySet

大多数谷歌搜索导致 people discovering that the POCO classes had some inheritance they forgot about. Someone 建议强制重新启用迁移。我在模型中根本没有任何继承,强制迁移只提供了一个重新创建的配置文件。

我总是会存储程序集限定的类型名称而不是类型本身。

一个 Type 实例,它不仅仅是一个名称,还有很多元数据,在 运行 期间可能很有趣,但存储起来毫无意义(即 序列化) 类型实例原样.

当你设置整个Type属性得到Type.AssemblyQualifiedName属性值:

instance.Type = typeof(X).AssemblyQualifiedName;

并且应该与整个类型一起工作的代码可以调用 Type.GetType(lock.Type) 再次构建一个 Type 实例。