.Net核心外键字段始终为空

.Net核心外键字段始终为空,.net,foreign-keys,relationship,core,.net,Foreign Keys,Relationship,Core,我们有一个.net核心api项目。外键模型总是从select查询返回null DBContext使用UseLazyLoadingProxies选项初始化。 外键关系在ContentTopic表中定义 外键定义为ContentTopic->TopicId=Topic->Id 在下面的示例中,主题始终返回null services.AddDbContext<VaultContext>(options =>options.UseLazyLoadingProxies().UseSqlS

我们有一个.net核心api项目。外键模型总是从select查询返回null

DBContext使用UseLazyLoadingProxies选项初始化。 外键关系在ContentTopic表中定义

外键定义为ContentTopic->TopicId=Topic->Id

在下面的示例中,主题始终返回null

services.AddDbContext<VaultContext>(options =>options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DBContext")));

[Table("ContentTopic")]
public class ContentTopic
{
    [Key]
    public long Id { get; set; }
    public long TopicId { get; set; }
    public long ContentId { get; set; }
    public DateTime CreateDate { get; set; }
    public bool IsInBody { get; set; }

    [ForeignKey("TopicId")]
    public virtual Topic Topic { get; set; }
}

UseLazyLoadingProxies扩展必须从OnConfigurang方法中的DBContext调用,而不是从Startup调用。cs

您的主题实体上的Id属性是否为long类型?是的,它很长。有许多foreignkey关系,所有这些关系都返回为null。。!我试着复制你的问题,但我这方面没有任何问题。在您的Vault环境中,您是否拥有所需的所有DbSet属性?我们已修复了非常奇怪的问题!实际上,我们已经在startup.cs中定义了UseLazyLoadingProxies扩展,但它不是解决方案,然后我们在OnConfigurang方法中从dbcontext调用了该扩展,它成功了!