1

Background: Project is a Data Import utility for importing data from tsv files into a EF5 DB through DbContext.

Problem: I need to do a lookup for ForeignKeys while doing the import. I have a way to do that but the retrieval if the ID is not functioning.

So I have a TSV file example will be

Code    Name    MyFKTableId
codevalue   namevalue   select * from MyFKTable where Code = 'SE'

So when I process the file and Find a '...Id' column I know I need to do a lookup to find the FK The '...' is always the entity type so this is super simple. The problem I have is that I don't have access to the properties of the results of foundEntity

string childEntity = column.Substring(0, column.Length - 2);
DbEntityEntry recordType = myContext.Entry(childEntity.GetEntityOfReflectedType());

DbSqlQuery foundEntity = myContext.Set(recordType.Entity.GetType()).SqlQuery(dr[column])

Any suggestion would be appreciated. I need to keep this generic so we can't use known type casting. The Id Property accessible from IBaseEntity so I can cast that, but all other entity types must be not be fixed

Note: The SQL in the MyFKTableId value is not a requirement. If there is a better option allowing to get away from SqlQuery() I would be open to suggestions.

SOLVED: Ok What I did was create a Class called IdClass that only has a Guid Property for Id. Modified my sql to only return the Id. Then implemented the SqlQuery(sql) call on the Database rather than the Set([Type]).SqlQuery(sql) like so.

IdClass x = ImportFactory.AuthoringContext.Database.SqlQuery<IdClass>(sql).FirstOrDefault();
4

1 に答える 1

0

解決済み: OK 私がしたことは、Id の Guid プロパティのみを持つ IdClass というクラスを作成することでした。Idのみを返すようにSQLを変更しました。次に、そのように Set([Type]).SqlQuery(sql) ではなく、データベースに SqlQuery(sql) 呼び出しを実装しました。

IdClass x = ImportFactory.AuthoringContext.Database.SqlQuery<IdClass>(sql).FirstOrDefault();
于 2013-04-25T13:22:37.707 に答える