。NET Core 2.2-实体框架-自定义分配[NotMapped]模型属性,带有DbSet.FromSql()

问题描述 投票:0回答:1

我有一个.NET Core 2.2 API项目。我正在尝试使用自定义属性设置EF模型类,这些属性未直接映射到表中的字段,将[[通常为NULL,但在单个实例中将被自定义分配。

(从概念上讲,这是我的模型:

public class SomeObject { [Key] public int ID { get; set; } [NotMapped] public string CustomField1 { get; set; } [NotMapped] public string CustomField2 { get; set; } }

在代码的其他地方,我基本上等于这:

return SomeObjects .FromSql($@"select *, <custom mapping here> as CustomField1, <custom mapping here> as CustomField2 from [table name]")

我希望映射器能够根据字段名称匹配在幕后将其弄清楚,但这是行不通的。

或者,我希望在Ruby on Rails或Laravel中使用map()方法,在这里我可以做这样的事情:

return SomeObjects .FromSql($@"select *, <custom mapping here> as CustomField1, <custom mapping here> as CustomField2 from [table name]") .Map( o => { o.CustomField1 = CustomField1, o.CustomField2 = CustomField2 });

如果存在,我不会感到惊讶,我只是想念它。我还没有找到任何文档,但也许我只是没有搜索正确的内容。

想法?

.net entity-framework .net-core asp.net-core-2.0
1个回答
1
投票
[尝试定义具有从实体类型CustomFieldX继承的其他SomeObjects属性的子类型,以利用公共属性和映射。我讨厌对SQL进行硬编码(违反ORM的主要目的之一),并且希望使用视图或存储过程,然后可以将子类型映射到该视图或存储过程。
© www.soinside.com 2019 - 2024. All rights reserved.