我首先使用 Entity Framework 4.3、POCO、数据库,但出现以下错误:
内部 .NET Framework 数据提供程序错误 1025。
问题:我认为我的查询表达了我的意图,但我似乎遇到了这个错误,所以我想知道是否有人知道我可以如何以不同的方式构建我的查询来绕过这个错误?
这是场景...
我有一个 SQL Server 2008 数据库,它有 2 个表 - A 和 B:
一个
- AId(整数 - 不为空 - 身份 - 主键)
- AName(nvarchar(10) - 不为空)
B
- BId(整数 - 不为空 - 身份 - 主键)
- SomeName(nvarchar(10) - 不为空)
- AId (int - not null - 外键连接表A中的AId)
然后我这样定义上下文:
public class DatabaseContext : DbContext
{
public DatabaseContext(string name)
: base(name)
{
Configuration.AutoDetectChangesEnabled = false;
As = Set<A>();
Bs = Set<B>();
}
public DbSet<A> As { get; private set; }
public DbSet<B> Bs { get; private set; }
}
实体类是这样的:
public class A
{
public int AId { get; set; }
public string AName { get; set; }
public virtual ICollection<B> Bs { get; private set; }
public void AddB(B b)
{
if (b == null)
{
throw new ArgumentNullException("b");
}
if (Bs == null)
{
Bs = new List<B>();
}
if (!Bs.Contains(b))
{
Bs.Add(b);
}
b.A = this;
}
}
public class B
{
public int BId { get; set; }
public A A { get; set; }
public string SomeName { get; set; }
}
现在查询...
我想要的是所有 As,其中每个“B SomeName”都在提供的名称列表中,所以我这样做:
var names = new[] {"Name1", "Name2"};
var ctx = new DatabaseContext("EFPlayingEntities");
var res = ctx.As.Where(a => a.Bs.Select(b => b.SomeName).All(names.Contains));
// Here I evaluate the query and I get:
// Internal .NET Framework Data Provider error 1025.
Console.WriteLine(res.Count());
为了清楚我的意思,如果表格数据是这样的:
AId,AName
1,A1
2,A2
3,A3
4,A4
BId,SomeName,AId
1,Name1,1
2,Name2,1
3,Name1,2
4,Name1,3
5,Name3,3
6,Name1,4
7,Name2,4
我希望返回 A1、A2 和 A4(这样上面的计数调用将返回 3)。
最佳答案
发生这种情况的原因很微妙。
Queryable.All
需要用 Expression
调用。仅传入方法“reference”会创建一个委托(delegate),随后 Enumerable.All
成为候选对象,而不是预期的 Queryable.All
。
这就是您作为答案发布的解决方案正常工作的原因。
编辑
所以如果你这样写语句,它会毫无异常(exception)地工作:
var res = ctx.As.Where(
a => a.Bs.Select(b => b.SomeName).All(b => names.Contains(b)));
关于c# - 如何绕过 "Internal .NET Framework Data Provider error 1025."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9513284/