简体   繁体   中英

EF4: Why isn't Translate() using Table mapping?

If I have db table like:

CREATE TABLE MyTable
(
MyTableId INT PRIMARY KEY,
MyTableName CHAR(10)
)

and entity in entity framework 4 (POCO, self tracking) defined as:

MyTable - maps to MyTable table
 - Id - maps to MyTableId
 - Name - maps to MyTableName

why does this query:

SqlConnection conn = (SqlConnection)((EntityConnection)context.Connection).StoreConnection;
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable", conn);
DbDataReader result = cmd.ExecuteReader();
var objResult = context.Translate<MyTable>(result);

fail saying:

The data reader is incompatible with the specified 'Project1.MyTable'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.

Shouldn't the Translate method take into account the table mappings defined in edmx? Is there any way to make this work without explicitly defining columns in query?

MSDN says:

The Translate method enables you to execute a standard ADO.NET query against a data source and translate the returned data rows into entity objects.

The supplied DbDataReader must contain data that maps to the requested entity type.

It isn't explicit whether it maps by name or via the map, but since this ADO.NET query isn't an entity query (and therefore isn't strictly tied to the abstraction layers), it seems reasonable that it might ignore it and opt for property-name matches.

I wonder if you should be writing an ESQL store query , rather than a TSQL database query? I would hope that an ESQL query is mapped via the model. This should be fairly simple; it could be just

var objResult = context.ExecuteStoreQuery<TEntity>(@"select * from MyTable");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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