代码之家  ›  专栏  ›  技术社区  ›  r3plica

EntityFramework未映射,但返回结果

  •  0
  • r3plica  · 技术社区  · 6 年前

    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
    END
    

    EntityFramework知道不映射它们,但当我使用存储过程时,我希望它们被映射。 有可能吗?我知道,我可以创造一个新的模型,然后用它来代替,但我想知道是否有更简单的方法?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Gert Arnold    6 年前

    行为 Database.SqlQuery ,无论是否有实体类型,都是 documented extensively

    创建将返回给定类型的元素的原始SQL查询。类型可以是具有与查询返回的列名称匹配的属性的任何类型,也可以是简单的基元类型。类型不必是实体类型。即使返回的对象类型是实体类型,上下文也不会跟踪此查询的结果。

    ,未提及与非映射属性相关的奇怪行为。这就是跑步。。。

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

    T 是映射的实体类型。当使用包含匹配属性的任何其他类型时,它会这样做。我觉得这有点奇怪,我甚至怀疑这是无意的行为。

    所以你不能用 Center 作为接收类型,但可以使用直接继承自的非映射类型 居中