2017-08-15 31 views
1

我最近从我的表中删除了一列ConversationId。当我开始调试我的服务,并试图挽救我得到一个错误:实体框架核心仍旧挑选旧列

Invalid column name 'ConversationId'.

代码:

public class AstootContext : DbContext 
{ 
    public AstootContext(DbContextOptions<AstootContext> options) 
     : base(options) 
    { } 

    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
    } 

    public DbSet<ServiceRequest> ServiceRequests { get; set; } 
} 

我的实体是这样的:

public class ServiceRequest 
{ 
    public int Id { get; set; } 
    public int SenderUserId { get; set; } 
    public int PriceTypeId { get; set; } 
    public decimal Price { get; set; } 
    public bool IsAccepted { get; set; } 
    public DateTime Created { get; set; } 
    public int MessageId { get; set; } 
} 

全部以引用ConversationId已从代码中删除,我已重建,但仍然出现此错误,我不明白为什么。

这是我的SQL Server表,你可以看到有没有ConversationId

enter image description here

有一个秘密的缓存,我需要删除或东西我要运行更新呢?

+0

我想你可以尝试运行'再次migrations'更新我没有使用迁移目前修改的列 –

+0

@HaHoang ,我的数据库没有反映我的外键结构 –

+0

听起来就像你有一个名为'Conversation'的实体,具有像'public ICollection ServiceRequests {get;组; '',不是吗? –

回答

3

EF Core是基于代码的ORM,其中最重要的是M-Mapper。它与实际的数据库结构无关,重要的是EF *认为它基于您的代码模型(实体类及其属性,结合数据注释,流畅配置和一组约定)。

所以这个问题应该来源于代码。由于您已删除明确的属性,因此应该由shadow property引起。而作为文档的链接解释,阴影属性通常按照惯例,从关系介绍:

Shadow properties can be created by convention when a relationship is discovered but no foreign key property is found in the dependent entity class. In this case, a shadow foreign key property will be introduced.

该文档还介绍了在不同情况下应用的命名规则。

可以以几种方式被引入称为ConversationId阴影属性,但根据所提供的信息,最可能的原因是已称为实体类由具有集合类型Conversation限定与ServiceRequest一个一对多关系导航属性:

public class Conversation 
{ 
    public int Id { get; set; } 
    // ... 
    public ICollection<ServiceRequest> ServiceRequests { get; set; } 
} 

哪根据你的评论确实如此。

为了完整性,这里有一些其它可能的情况产生这样的属性:

(1)任何集合导航属性在Conversation,参考导航属性在ServiceRequest

public class Conversation 
{ 
    public int Id { get; set; } 
    // ... 
} 

public class ServiceRequest 
{ 
    // ... 
    public Conversation Conversation { get; set; } 
} 

(2)无导航属性在ConversationServiceRequest,流利配置:

modelBuilder.Entity<Conversation>() 
    .HasMany<ServiceRequest>(); 

modelBuilder.Entity<ServiceRequest>() 
    .HasOne<Conversation>(); 

或上述的变体。

(3)没有关系参与,shadow属性通过流畅的配置创建:

modelBuilder.Entity<ServiceRequest>() 
    .Property<int>("ConversationId");