简体   繁体   English

在实体框架中将SQL数据绑定到模型属性

[英]Bind the SQL data to model property in Entity Framework

Good day to you. 祝你有美好的一天。 I am returning a result as preceding from SQL stored procedure. 我从SQL存储过程中返回了前面的结果。

在此处输入图片说明

Now I am binding the data as follows. 现在,我将数据绑定如下。

 List<EventPhotos> resList = null;
 resList = pe.Database.SqlQuery<EventPhotos>(sqlQuery, param).ToList();

Following is my model. 以下是我的模型。

public partial class EventPhotos
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public EventPhotos()
        {

        }        

        public EventInfo EventInformation { get; set; }
        public ImageInfo ImageInformation { get; set; }

    }

    public class EventInfo
    {
        public string EventID { get; set; }

        public string EventName { get; set; }

        public bool IsJoint { get; set; }
    }
    public class ImageInfo
    {
        public string ImageID { get; set; }

        public string EventMediaFolder { get; set; }

        public string Image { get; set; }

        public string DateTime { get; set; }

        public string ImageDescription { get; set; }

        public string Action { get; set; }
    }

But when I run this, nothing gets mapped to my properties EventInformation and ImageInformation. 但是,当我运行此代码时,没有任何内容映射到我的属性EventInformation和ImageInformation。 Is there anyway to achieve this. 无论如何要实现这一目标。 I know this can be done if we use dataAdapter and assign names to the each table retrieved. 我知道如果我们使用dataAdapter并为检索到的每个表分配名称,就可以做到。 But I want to use the Entity framework. 但是我想使用实体框架。 Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。

Try and add the table and column attributes 尝试添加表和列属性

public class EventInfo
{

    [Column(Name = "EventID")]
    public string EventID { get; set; }


    [Column(Name = "EventName")]
    public string EventName { get; set; }


    [Column(Name = "IsJoint")]
    public bool IsJoint { get; set; }
}
public class ImageInfo
{

    [Column(Name = "ImageID" )]
    public string ImageID { get; set; }


    [Column(Name = "EventMediaFolder")]
    public string EventMediaFolder { get; set; }


    [Column(Name = "Image")]
    public string Image { get; set; }


    [Column(Name = "DateTime")]
    public string DateTime { get; set; }


    [Column(Name = "ImageDescription")]
    public string ImageDescription { get; set; }


    [Column(Name = "Action")] 
    public string Action { get; set; }
}

I use the below code to do the mapping 我使用以下代码进行映射

 returnItems = context.ExecuteQuery<EventPhotos>(sql).ToList();

Database.SqlQuery : 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. 即使返回的对象类型是实体类型,上下文也永远不会跟踪此查询的结果。

It means, that the column names of the resultset of your stored procedure must match with the names of properties defined in your POCO class EventPhotos . 这意味着存储过程的结果集的列名称必须与POCO类EventPhotos定义的属性名称匹配。 So you must create the class EventPhotos with exact names of the returned columns instead of adding two properties - the nested properties names cannot be found by the Database.SqlQuery method. 所以,你必须创建一个类EventPhotos与返回的列,而不是添加两个属性的准确名称-嵌套特性名称不能被发现Database.SqlQuery方法。

SqlQuery does not support complex types. SqlQuery不支持复杂类型。

See a similar question here . 在这里看到类似的问题

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

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