将(System.Data.SqlClient)转换为Entity

时间:2016-09-13 06:57:41

标签: c# sql-server entity-framework

我想尝试一次尝试多选,

我找到了一种使用SqlCommand的方法并且它有效,但我看到的是实体的方式,请帮助我找到实体的方法或将此代码转换为我想要的。

string commandText = @"SELECT Id, ContactId
FROM dbo.Subscriptions;

SELECT Id, [Name]
FROM dbo.Contacts;";


List<Subscription> subscriptions = new List<Subscription>();
List<Contact> contacts = new List<Contact>();

using (SqlConnection dbConnection = new SqlConnection(@"Data Source=server;Database=database;Integrated Security=true;"))
using (SqlCommand dbCommand = new SqlCommand(commandText, dbConnection))
{
    dbCommand.CommandType = CommandType.Text;
    dbConnection.Open();

    SqlDataReader reader = dbCommand.ExecuteReader();

    while (reader.Read())
    {
        subscriptions.Add(
            new Subscription()
            {
                Id = (int)reader["Id"],
                ContactId = (int)reader["ContactId"]
            });
    }

    reader.NextResult();

    while (reader.Read())
    {
        contacts.Add(
            new Contact()
            {
                Id = (int)reader["Id"],
                Name = (string)reader["Name"]
            });
    }

    dbConnection.Close();
}

0 个答案:

没有答案