.net-core - .Net Core 外键字段始终为空
问题描述
我们有一个 .net core 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; }
}
解决方案
UseLazyLoadingProxies 扩展必须在 OnConfiguring 方法中从 DBContext 调用,而不是从 Startup.cs
推荐阅读
- filepond - FilePond A11Y 问题
- sql - 从sql server中的xml中提取数据
- lua - 如何在加载到 Lua 的 C 库中进行 Lua 断点/单步调试
- javascript - 如何从移动设备上的 keydown 事件中获取密钥?
- c++ - Qt Creator vcpkg 集成
- r - dbplyr filter() 和惰性求值
- oracle - 如何将 Oracle CDC Source Connector 生成的 CDC 事件摄取到 Snowflake 中
- protobuf-net - 如何通过项目文件启用 protobuf-net.BuildTools OneOf 支持?
- python - 如何找出在 Python 中处理了多少向量?
- performance - 如何在jmeter中处理多种语言?