针对 DataContext 和接口的通用函数约束使用 lambda 时出现 NotSupportedException
我不完全确定标题的措辞是否正确,但情况是这样的...今天我在尝试为 Linq to Sql 创建通用保存函数时注意到,当我对数据上下文选择使用 lambda 时。它打破了具有另一个通用接口的类型约束的通用函数。但是,它可以很好地使用 LINQ 语法。我提出这个问题或多或少是为了了解为什么会发生这种情况。
抛出的异常是:
NotSupportedException
不支持接口成员IEntity`1.ID的映射。
涉及的代码看起来与此类似(简化):
导致错误的函数是 GenericFunc
//Assume SomeTable is generated from a DBML L2S file. //partial extension of the L2S class public partial class SomeTable : IEntity<SomeTable> { } //interface to expose shared properties in these entity classes public interface IEntity<T> where T : class { //isolated application, int Primary key for all these tables. int ID { get; set; } } //simple helper class for L2S stuff. public class Repository { //helper for inserting/updating data.. public void SaveSomeTable(SomeTable data) { SomeDataContext db = new SomeDataContext(); GenericFunc<SomeTable>(db.SomeTables, data); } //toy function for this example public void GenericFunc<T>(System.Data.Linq.Table<T> table, T data) where T : class, IEntity<T>, new () { //note the generic type constraints... //in this case, all table entities conform to the IEntity<T> class. //breaks var row = table.SingleOrDefault(o => o.ID == data.ID); //works var row1 = (from item in table where item.ID == data.ID select item).SingleOrDefault(); //... more stuff. } }
I'm not entirely sure the title is properly worded, but here's the situation... I noticed today while trying to create a generic save function for Linq to Sql that when I use lambda against a data context select. It breaks within a generic function with a type constraint of another generic interface. However, it works fine with LINQ syntax. I'm posing this question more or less to get an understanding of why this happens.
The exception being thrown is:
NotSupportedException
The mapping of interface member IEntity`1.ID is not supported.
The code involved looks similar to this (simplified):
The error causing function is GenericFunc<T>
//Assume SomeTable is generated from a DBML L2S file. //partial extension of the L2S class public partial class SomeTable : IEntity<SomeTable> { } //interface to expose shared properties in these entity classes public interface IEntity<T> where T : class { //isolated application, int Primary key for all these tables. int ID { get; set; } } //simple helper class for L2S stuff. public class Repository { //helper for inserting/updating data.. public void SaveSomeTable(SomeTable data) { SomeDataContext db = new SomeDataContext(); GenericFunc<SomeTable>(db.SomeTables, data); } //toy function for this example public void GenericFunc<T>(System.Data.Linq.Table<T> table, T data) where T : class, IEntity<T>, new () { //note the generic type constraints... //in this case, all table entities conform to the IEntity<T> class. //breaks var row = table.SingleOrDefault(o => o.ID == data.ID); //works var row1 = (from item in table where item.ID == data.ID select item).SingleOrDefault(); //... more stuff. } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上并不是 lambda 问题。这是也可以工作的非查询语法:
基本上编译为与查询表达式相同的代码。
我强烈怀疑这是
SingleOrDefault
(内部)的不同代码路径,这会带来问题。This isn't really a lambda issue. Here's non-query-syntax which will work too:
That compiles to the same code as the query expresion, basically.
I strongly suspect it's a different code path for
SingleOrDefault
(internally) which gives problems.