简体   繁体   English

EntityFramework未映射但返回结果

[英]EntityFramework NotMapped but return results

This might sound odd, but I have an entity: 这听起来可能很奇怪,但是我有一个实体:

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; }
}

When listing, editing, creating, etc. The [NotMapped] properties should not be mapped in the database. 列出,编辑,创建等时。 [NotMapped]属性不应映射到数据库中。 But, I have a stored procedure that populates these properties: 但是,我有一个填充这些属性的存储过程:

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
END

EntityFramework knows not to map them, but when I use my SPROC I would like them to be mapped. EntityFramework知道不映射它们,但是当我使用SPROC时,我希望它们被映射。 Is that possible? 那可能吗? I realise, I could just create a new model and use that instead but I want to know if there is something easier? 我知道,我可以创建一个新模型并使用它,但是我想知道是否有更简单的方法?

The behavior of Database.SqlQuery , with or without entity types, is documented extensively : 不论有没有实体类型, Database.SqlQuery的行为都有大量记录

Creates a raw SQL query that will return elements of the given type. 创建一个原始SQL查询,该查询将返回给定类型的元素。 The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. 该类型可以是具有与查询返回的列名相匹配的属性的任何类型,也可以是简单的原始类型。 The type does not have to be an entity type. 该类型不必是实体类型。 The results of this query are never tracked by the context even if the type of object returned is an entity type. 即使返回的对象类型是实体类型,上下文也永远不会跟踪此查询的结果。

Yet , the somewhat quirky behavior with non-mapped properties isn't mentioned. 但是 ,没有提到具有非映射属性的古怪行为。 Which is that running... 正在运行...

_context.Database.SqlQuery<T>($"exec {storedProcedureName}")

...doesn't populate not-mapped properties when T is a mapped entity type. T为映射实体类型时,不会填充未映射的属性。 It does when any other type is used that contains the matching properties. 当使用任何其他包含匹配属性的类型时,它将执行此操作。 I think that's a bit weird and I even suspect it's unintended behavior. 我认为这有点怪异,甚至怀疑这是意外行为。

So you can't use Center as the receiving type, but you can use an non-mapped type directly inheriting from Center . 因此,您不能使用Center作为接收类型,但可以使用直接继承自Center的非映射类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM