简体   繁体   English

实体框架中LINQ-to-SQL的ExecuteCommand相当于什么?

[英]What is the equivalent of LINQ-to-SQL's ExecuteCommand in Entity Framework?

I am porting an application running on LINQ-to-SQL to Entity Framework , and am having trouble finding an equivalent to ExecuteCommand : 我正在将运行在LINQ-to-SQL上的应用程序移植到Entity Framework ,并且无法找到与ExecuteCommand等效的内容:

db.ExecuteCommand("INSERT Infos (Title) VALUES ('this is an added title')");

I found this site which tells me that it is possible to implement ExecuteCommand as an extension method on your ObjectContext, leaving the existing code unchanged by adding this code to a static method in your project: 我发现这个站点告诉我可以在ObjectContext上实现ExecuteCommand作为扩展方法 ,通过将此代码添加到项目中的静态方法来保持现有代码不变:

public static int ExecuteCommand(this ObjectContext objectContext,
                                string command) {
    DbConnection connection = ((EntityConnection)objectContext.Connection).StoreConnection;
    bool opening = (connection.State == ConnectionState.Closed);
    if (opening)
        connection.Open();

    DbCommand cmd = connection.CreateCommand();
    cmd.CommandText = command;
    cmd.CommandType = CommandType.StoredProcedure;
    try {
        return cmd.ExecuteNonQuery();
    }
    finally {
        if (opening && connection.State == ConnectionState.Open)
            connection.Close();
    }
}

But I've tried putting it (1) in the class where I use it, (2) in the Entity generated class file, (3) in a static class in the project, but always get various errors. 但我已经尝试将它(1)放在我使用它的类中,(2)在Entity生成的类文件中,(3)在项目中的静态类中,但总是会得到各种错误。 Where do I need to put this method exactly? 我在哪里需要准确地使用这种方法?

The above site also said: 上述网站称:

ExecuteCommand is being considered as an enhancement for a future release of the Entity Framework ExecuteCommand被视为对Entity Framework未来版本的增强

Does ExecuteCommand() exist in the newest version of Entity Framework? ExecuteCommand()是否存在于最新版本的Entity Framework中? I am using the Entity Framework that was installed with Visual Studio 2008 SP1. 我正在使用随Visual Studio 2008 SP1一起安装的实体框架。

.NET Framework 4的ObjectContext类具有ExecuteStoreCommandExecuteStoreQuery方法,这些方法似乎正是您所寻找的。

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

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