这可能听起来很奇怪,但我有一个实体:
public class Center : Archive
{
public int Id { get; set; }
[MaxLength(50)] public string ExternalId { get; set; }
[Required] [MaxLength(150)] public string Name { get; set; }
[MaxLength(255)] public string Description { get; set; }
[MaxLength(50)] public string Address1 { get; set; }
[MaxLength(50)] public string Address2 { get; set; }
[MaxLength(50)] public string Address3 { get; set; }
[MaxLength(50)] public string Address4 { get; set; }
[MaxLength(10)] public string PostCode { get; set; }
[MaxLength(100)] public string CollectionPointContact { get; set; }
[MaxLength(50)] public string CollectionPointTelephone { get; set; }
[MaxLength(50)] public string CollectionPointFax { get; set; }
[MaxLength(255)] public string CollectionPointEmail { get; set; }
[NotMapped] public int Due { get; set; }
[NotMapped] public int Today { get; set; }
[NotMapped] public int Expected { get; set; }
[NotMapped] public int Planned { get; set; }
public int CompanyId { get; set; }
public Company Company { get; set; }
public IList<Collection> Collections { get; set; }
}在列出、编辑、创建等操作时,不应在数据库中映射[NotMapped]属性。但是,我有一个存储过程来填充这些属性:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ListCentersByCompany]
@CompanyId [int]
AS
BEGIN
select ce.*,
SUM(CASE WHEN co.PlannedCollectionDate < CONVERT(DATE, GETDATE()) THEN 1 ELSE 0 END) AS Due,
SUM(CASE WHEN co.PlannedCollectionDate = CONVERT(DATE, GETDATE()) THEN 1 ELSE 0 END) AS Today,
SUM(CASE WHEN co.PlannedCollectionDate = DATEADD(DAY, 1, CONVERT(DATE, GETDATE())) THEN 1 ELSE 0 END) AS Expected,
SUM(CASE WHEN co.PlannedCollectionDate > DATEADD(DAY, 1, CONVERT(DATE, GETDATE())) THEN 1 ELSE 0 END) AS Planned
from Centers ce join
Collections co
on ce.Id = co.CenterId
WHERE ce.CompanyId = @CompanyId
group by
ce.Id,
ce.ExternalId,
ce.Name,
ce.Description,
ce.Address1,
ce.Address2,
ce.Address3,
ce.Address4,
ce.PostCode,
ce.CollectionPointContact,
ce.CollectionPointEmail,
ce.CollectionPointFax,
ce.CollectionPointTelephone,
ce.CompanyId,
ce.CreatedById,
ce.ModifiedById,
ce.DateCreated,
ce.DateModified
ENDEntityFramework知道不映射它们,但是当我使用我的SPROC时,我希望映射它们。这有可能吗?我意识到,我可以创建一个新的模型并使用它,但我想知道是否有更简单的东西?
发布于 2019-01-19 21:34:28
无论是否具有实体类型,Database.SqlQuery的行为都是documented extensively
创建一个原始SQL查询,该查询将返回给定类型的元素。该类型可以是具有与查询返回的列名相匹配的属性的任何类型,也可以是简单的基元类型。类型不必是实体类型。即使返回的对象类型是实体类型,此查询的结果也不会被上下文跟踪。
然而,没有提到非映射属性的一些奇怪的行为。那就是奔跑。
_context.Database.SqlQuery<T>($"exec {storedProcedureName}")...当T是映射的实体类型时,不填充未映射的属性。当使用包含匹配属性的任何其他类型时,就会发生这种情况。我认为这有点奇怪,我甚至怀疑这是一种无意的行为。
所以不能使用Center作为接收类型,但是可以使用直接从Center继承的非映射类型。
https://stackoverflow.com/questions/54237662
复制相似问题