我们有一个.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; }
}
答案 0 :(得分:1)
UseLazyLoadingProxies扩展必须在OnConfiguring方法中从DBContext而不是从Startup.cs中调用