使用 EntityDataReader 从数据库中读取实体对象
Read Entity objects from database using EntityDataReader
由于某些原因,我需要使用 ADO.Net.
直接从数据库中读取实体对象
我从 Microsoft documentation 中找到了以下片段。我想知道是否有任何方法可以使用 EntityDataReader
将整行读入 Onject(此示例中的 'contact'),而不是将每个字段映射到每个 属性?我的意思是,不是一一读取 Contact.Id
和 Contact.Name
以及其他字段,是否有任何方法可以将一行读取到一个对象中?
using (EntityConnection conn =
new EntityConnection("name=AdventureWorksEntities"))
{
conn.Open();
string esqlQuery = @"SELECT VALUE contacts FROM
AdventureWorksEntities.Contacts AS contacts
WHERE contacts.ContactID == @id";
// Create an EntityCommand.
using (EntityCommand cmd = conn.CreateCommand())
{
cmd.CommandText = esqlQuery;
EntityParameter param = new EntityParameter();
param.ParameterName = "id";
param.Value = 3;
cmd.Parameters.Add(param);
// Execute the command.
using (EntityDataReader rdr =
cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
// The result returned by this query contains
// Address complex Types.
while (rdr.Read())
{
// Display CustomerID
Console.WriteLine("Contact ID: {0}",
rdr["ContactID"]);
// Display Address information.
DbDataRecord nestedRecord =
rdr["EmailPhoneComplexProperty"] as DbDataRecord;
Console.WriteLine("Email and Phone Info:");
for (int i = 0; i < nestedRecord.FieldCount; i++)
{
Console.WriteLine(" " + nestedRecord.GetName(i) +
": " + nestedRecord.GetValue(i));
}
}
}
}
conn.Close();
}
你最干净的选择是按照@herosuper
的建议使用EntityFramework执行你的查询
在您的示例中,您需要执行以下操作:
EntityContext ctx = new EntityContext();
var contacts= ctx.Contacts
.SqlQuery("SELECT * FROM AdventureWorksEntities.Contacts AS contacts"
+ "WHERE contacts.ContactID =@id", new SqlParameter("@id", 3)).ToList();
从这里,您将能够:
var myvariable = contacts[0].ContactID;//zero is index of list. you can use foreach loop.
var mysecondvariable = contacts[0].EmailPhoneComplexProperty;
或者,您可以通过以下方式跳过整个 SQL 字符串:
EntityContext ctx = new EntityContext();
var contact= ctx.Contacts.Where(a=> a.ContactID ==3).ToList();
我假设查询 returns 不止一条记录,否则您只需使用 FirstOrDefault()
而不是 Where()
由于某些原因,我需要使用 ADO.Net.
直接从数据库中读取实体对象我从 Microsoft documentation 中找到了以下片段。我想知道是否有任何方法可以使用 EntityDataReader
将整行读入 Onject(此示例中的 'contact'),而不是将每个字段映射到每个 属性?我的意思是,不是一一读取 Contact.Id
和 Contact.Name
以及其他字段,是否有任何方法可以将一行读取到一个对象中?
using (EntityConnection conn =
new EntityConnection("name=AdventureWorksEntities"))
{
conn.Open();
string esqlQuery = @"SELECT VALUE contacts FROM
AdventureWorksEntities.Contacts AS contacts
WHERE contacts.ContactID == @id";
// Create an EntityCommand.
using (EntityCommand cmd = conn.CreateCommand())
{
cmd.CommandText = esqlQuery;
EntityParameter param = new EntityParameter();
param.ParameterName = "id";
param.Value = 3;
cmd.Parameters.Add(param);
// Execute the command.
using (EntityDataReader rdr =
cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
// The result returned by this query contains
// Address complex Types.
while (rdr.Read())
{
// Display CustomerID
Console.WriteLine("Contact ID: {0}",
rdr["ContactID"]);
// Display Address information.
DbDataRecord nestedRecord =
rdr["EmailPhoneComplexProperty"] as DbDataRecord;
Console.WriteLine("Email and Phone Info:");
for (int i = 0; i < nestedRecord.FieldCount; i++)
{
Console.WriteLine(" " + nestedRecord.GetName(i) +
": " + nestedRecord.GetValue(i));
}
}
}
}
conn.Close();
}
你最干净的选择是按照@herosuper
的建议使用EntityFramework执行你的查询在您的示例中,您需要执行以下操作:
EntityContext ctx = new EntityContext();
var contacts= ctx.Contacts
.SqlQuery("SELECT * FROM AdventureWorksEntities.Contacts AS contacts"
+ "WHERE contacts.ContactID =@id", new SqlParameter("@id", 3)).ToList();
从这里,您将能够:
var myvariable = contacts[0].ContactID;//zero is index of list. you can use foreach loop.
var mysecondvariable = contacts[0].EmailPhoneComplexProperty;
或者,您可以通过以下方式跳过整个 SQL 字符串:
EntityContext ctx = new EntityContext();
var contact= ctx.Contacts.Where(a=> a.ContactID ==3).ToList();
我假设查询 returns 不止一条记录,否则您只需使用 FirstOrDefault()
而不是 Where()