EF6 - 实体不属于我的上下文?

时间:2015-07-16 14:22:06

标签: entity-framework ef-code-first

我的数据库有两个指向它的上下文 到目前为止,这些背景是完全分开的。我的问题是,我无法使用第二个上下文...它被创建得很好,所有表都在那里等等,但每当我查询它时,就会兴旺!

详细说明:
下面是背景:

public class ServiceSupportDbContext : DbContext
{
    public ServiceSupportDbContext(string connectionString)
        : base(connectionString)
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }
    public ServiceSupportDbContext() { }
    public DbSet<ImportItem> ImportItems { get; set; }
}

这是实体:

[Table("DIPItems")]
public class ImportItem
{
    internal string ExceptionMessage { get; set; }
    [Key]
    public Guid Id { get; set; }
    public Guid ContextId { get; set; }
    public DateTime Created { get; set; }
    public DateTime? LastUpdated { get; set; }
    public string LastKnownHost { get; set; }
    public string DocumentJson { get; set; }
    public string SourceFilePath { get; set; }
    public string DestinationFilePath { get; set; }
    [NotMapped]
    public string FullExceptionMessage
    {
        get
        {
            return ExceptionMessage;
        }
        set
        {
            ExceptionMessage = (ExceptionMessage == null ? value : ExceptionMessage + "\r\n" + value);
        }
    }
    public ImportItemStatus Status { get; set; }
    public int RetryCount { get; set; }
    public int ProcessId { get; set; }
    [Timestamp]
    public byte[] ConcurrencyCheck { get; set; }
}

[Flags]
public enum ImportItemStatus
{
    Waiting = 0,
    Copying = 1,
    Copied = 3,
    ExtractingContent = 4,
    ContentExtracted = 12,
    Indexing = 16,
    Indexed = 48,
    Hashing = 64,
    Hashed = 192,
    Error = -1
}

正如你可以看到这个上下文很简单,事情应该工作正常,但我不知道为什么,我一直在打&#34;实体类型不是当前上下文模型的一部分&#34;壁...

任何人都知道如何解决这个问题?也许解决这个烂摊子?

编辑1:
种子运行也很好......

2 个答案:

答案 0 :(得分:0)

将它放在您的自定义DBContext上:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ImportItem>().ToTable("ImportItem");
}

答案 1 :(得分:0)

我明白了。我没有注意到正在使用的连接字符串是一个EF旧式conn字符串...它指向一个旧的,但仍然存在的模型......