Code First - EntityType 'Compiler Results' 没有定义键。为此 EntityType 定义键

Code First - EntityType 'Compiler Results' has no key defined. Define the key for this EntityType

这是完整的错误:

{"One or more validation errors were detected during model generation:\r\n\r\nContentManager.CompilerResults: : EntityType 'CompilerResults' has no key defined. Define the key for this EntityType.\r\nCompilerResults: EntityType: EntitySet 'CompilerResults' is based on type 'CompilerResults' that has no keys defined.\r\n"}

这是一个奇怪的错误。首先,我在该名称空间 (ContentManager.CompilerResults) 中没有 class 调用 CompilerResults。其次,没有"EntityType 'CompilerResults'"。这是我创建的名为 "ContextEntityPair":

的实体 class
[Table("context_entity_pair")]
public class ContextEntityPair
{
    [Column("id")]
    [Key]
    public Guid Id
    {
        get;
        set;
    }

    [Column("context_name")]
    public string ContextName { get; set; }

    [Column("entity_compiler_results")]
    public CompilerResults EntityCompilerResults { get; set; }

    public ContextEntityPair(Guid id, string contextName, CompilerResults entityCompilerResults)
    {
        this.Id = id;
        this.ContextName = contextName;
        this.EntityCompilerResults = entityCompilerResults;
    }
}

我觉得我正在尽我所能避免 'no key defined' 错误。我使用名称 "Id",我将它设置为 属性,我什至给它一个 [Key] 属性。但由于某种原因,它没有给出关于 Id 不是密钥的错误。它说 'CompilerResults' 没有没有意义的密钥。这是我的上下文:

namespace Laserfiche.BusinessEntities.ContentManager.ReadStore.EF
{
    public class EntityCompilerResultsContext : DbContext
    {
        public EntityCompilerResultsContext()
            : base("EFEntityCompilerResults")
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EntityCompilerResultsContext>());
        }

        public DbSet<ContextEntityPair> ContextEntityPairs { get; set; }
    }
}

我正在为某些 CRUD 方法使用存储库。下面以 Insert 方法作为示例方法开始:

public class EntityContextRepository
{
    private readonly Type _entityType;
    private EntityCompilerResultsContext _db = new EntityCompilerResultsContext();

    public EntityContextRepository()
    {
        _entityType = typeof(ContextEntityPair);
    }

    public async Task InsertAsync(ContextEntityPair pair)
    {
        try
        {
            Ensure.IsNotNull(pair, "pair");
            _db.ContextEntityPairs.Add(pair);
            await _db.SaveChangesAsync();
        }
        catch (DbEntityValidationException dbEx)
        {
            DbEntityValidationExceptionHandle(dbEx);
        }
    }
}

最后是我 运行 的测试代码(有一些 CodeDOM 的东西——忽略它。重要的部分是我定义 'entityResults1' 的地方以及 [=35 中的所有内容=] 及以下):

string entityName = "Curtain";

EntityGenerator.EntityFieldInfo entityField1 = new EntityGenerator.EntityFieldInfo("Color", typeof(string), RelationshipType.NoRelation);
EntityGenerator.EntityFieldInfo entityField2 = new EntityGenerator.EntityFieldInfo("Size", typeof(int), RelationshipType.NoRelation);

ICollection<EntityGenerator.EntityFieldInfo> listOfProps = new List<EntityGenerator.EntityFieldInfo> { entityField1, entityField2 };
EntityGenerator.CreateEntityClass(listOfProps, entityName);
CompilerResults entityResults1 = EntityGenerator.GetCompiledEntity(entityName);

GenericEntity entityInstance1 = (GenericEntity) EntityGenerator.CreateInstanceOfEntity(entityResults1, entityName);

SetObjectField(entityInstance1, "Color", "Green");
SetObjectField(entityInstance1, "Size", 20);

string contextName = "EFEntityTest_v1";
Guid testGuid1 = new Guid("00000000000000000000000000000001");

ContextEntityPair entityCompilerResultsPair = new ContextEntityPair(testGuid1, contextName, entityResults1);
EntityContextRepository contextEntityRepo = new EntityContextRepository();
contextEntityRepo.InsertAsync(entityCompilerResultsPair).Wait();

它在行的 InsertAsync 内部中断:

_db.ContextEntityPairs.Add(pair);

你的 public CompilerResults EntityCompilerResults { get; set; } 没有标有 ComplexType 属性,它也不是标量 属性,也不是引用。您期望它的表现如何?