【发布时间】:2020-04-02 08:38:48
【问题描述】:
我有两个表 articles 和 content(一对一关系)。 content 表包含 JSON 格式 (PostgreSQL) 的文章正文。出于性能原因,表格被拆分,因为内容太大。
文章表:
| Column | Type |
|-----------------|--------|
| id | int |
| title | string |
| *other columns* | xxx |
内容表:
| Column | Type |
|-----------|-------|
| articleId | int |
| content | jsonb |
我正在尝试将这些表映射到单个实体 Article:
namespace Domain.Models
{
[Table("articles")]
public class Article
{
[Column("id")]
public int Id { get; set; }
[Column("title")]
public string Title { get; set; }
[Column("content")]
public string Content { get; set; }
<...> // other columns
}
}
我的DbContext:
namespace Domain
{
public class MasterDbContext : DbContext
{
public DbSet<Article> Articles { get; set; }
}
}
问题: 如何从content 表映射Content 属性?
我的愚蠢解决方案:我刚刚创建了一个非物化视图并将Article 实体映射到这个“表”:
CREATE VIEW articles_with_content AS
SELECT a.*, c.content
FROM articles AS a
JOIN content AS c ON c."articleId" = a.id;
有人可以提出更好的解决方案吗?
【问题讨论】:
标签: c# postgresql entity-framework database-design entity-framework-core