modelBuilder.Entity<WishlistLine>() .HasKey(w => w.PersistenceKey) .Property(w => w.PersistenceKey) .HasColumnName("WishlistLineId");
public IEnumerable<WishlistLine> FetchWishlistLinesUsinglogonName(string logonName) { return GetFromrawsql(@" SELECT wl.* FROM WishlistLines wl INNER JOIN Accounts a ON wl.AccountId = a.AccountId LEFT JOIN Users u ON u.AccountId = a.AccountId WHERE u.logonName = @p0",logonName); } protected IEnumerable<TEntity> GetFromrawsql(string sqlQuery,params object[] parameters) { return _dbSet.sqlQuery(sqlQuery,parameters).ToList(); }
我可以通过EF将WishlistLines“保存”到数据库中,没有任何问题.当我运行此查询但我收到此错误:
数据读取器与指定的“DataAccessLayer.DatabaseContext.WishlistLine”不兼容.类型为“PersistenceKey”的成员在数据读取器中没有相应的具有相同名称的列.
我知道使用DbSet< T> .sqlQuery()会将返回的数据映射到实体,但它似乎忽略了DbModel配置.从错误消息中判断(猜测)正在使用错误的数据读取器.
所以:
A)我做错了吗?
B)有没有办法利用EF的DbModel感知实体映射器?
解决方法
引自http://entityframework.codeplex.com/workitem/233:
The sqlQuery method is designed not to take any mapping into account,
including mapping that is applied using attributes. It simply matches
the column names from the results with property names in the object.
If the column names don’t match you will need to use a column alias
(AS keyword in sql Server) to rename the column in the results.We agree that it would be useful to have the option to make sqlQuery
honor Column attributes so we’re keeping this issue open and putting
it on our backlog for future consideration.
因此,唯一的解决方法似乎是在SQL查询中使用显式AS别名而不是*,将您的属性名称指定为列别名:
return GetFromrawsql(@" SELECT wl.WishlistLineId AS PersistenceKey,wl.someOtherColumn AS SomeOtherProperty,... ..." // ...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。