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

时间:2019-08-26 13:06:34

标签: .net foreign-keys relationship core

我们有一个.net核心api项目。外键模型始终从选择查询中返回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; }
}

1 个答案:

答案 0 :(得分:1)

UseLazyLoadingProxies扩展必须在OnConfiguring方法中从DBContext而不是从Startup.cs中调用