简体   繁体   English

EDMX 数据库将两个过程映射到同一个 ComplexType

[英]EDMX database mapping two procedures to the same ComplexType

I am creating an .edmx into my MVC project.我正在为我的 MVC 项目创建一个 .edmx。 I have mapped two procedures that returns movies information from database.我已经映射了两个从数据库返回电影信息的过程。

The first procedure, which is getMoviesByName, returns only Id and Name.第一个过程是 getMoviesByName,它只返回 Id 和 Name。 The second procedure, which is getAllMovies, returns Id, Name and CreatedDate.第二个过程是 getAllMovies,它返回 Id、Name 和 CreatedDate。

I would like to both procedures to returns data as a ComplexType called Movie.我想这两个程序都将数据作为名为 Movie 的 ComplexType 返回。

I am instantiating my MyDbEntities and trying to use both procedures according to a condition just like in the example below but it fails when variable isLookingByName is true with the following message:我正在实例化我的 MyDbEntities 并尝试根据如下示例中的条件使用这两个过程,但是当变量isLookingByName为真时失败并显示以下消息:

A member of the type, 'CreatedDate', does not have a corresponding column in the data reader with the same name. 'CreatedDate' 类型的成员在数据读取器中没有对应的同名列。

Example:例子:

var db = new MyDbEntities();
if(isLookingByName) 
{ 
    return db.getMoviesByName(name).ToList();
}

return db.getAllMovies().ToList();

So whenever the condition is true it throws that exception.因此,只要条件为真,它就会抛出该异常。 The thing is that I cant change the first procedure to add CreatedDate .问题是我无法更改添加CreatedDate的第一个过程。

Just to let you guys know in my View I display all those three information Id, Name and CreatedDate.只是为了让你们知道在我的视图中,我显示了所有这三个信息 Id、Name 和 CreatedDate。 When CreatedDate is null it is not displayed and I am fine with that.当 CreatedDate 为 null 时,它不会显示,我对此很好。

Someone knows how should I proceed?有人知道我应该如何进行吗?

Thank you very much!非常感谢!

The workaround is to use ADO.NET解决方法是使用 ADO.NET

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    using (SqlCommand command = new SqlCommand(
    "SELECT Id, Name and CreatedDate  FROM movies WHERE id= @id", connection))
    {

        command.Parameters.Add(new SqlParameter("@id", id));

        SqlDataReader reader = command.ExecuteReader();
        if(reader.HasRows())
        {
            var id= reader["Id"].ToString();
            var name= reader["Name"].ToString();
        }
    }
}

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

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