gpt4 book ai didi

c# - 使用 EntityConnection、EntityCommand 和 EntityDataReader 时出现 EntitySqlException

转载 作者:太空宇宙 更新时间:2023-11-03 15:31:23 26 4
gpt4 key购买 nike

我在使用 EntityConnection 时收到此错误,我不知道如何解决。使用 ObjectContext 时,我可以设置属性 DefaultContainerName 并且它可以工作,但现在我不知道是什么导致了问题以及如何解决它。感谢您的帮助。

完整的错误看起来像这样->

An exception of type 'System.Data.Entity.Core.EntitySqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: 'Animals' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 1, column 21.

我的代码看起来像这样,当点击 cmd.ExecuteReader() -> 时抛出异常

using (EntityConnection conn = new EntityConnection("name=dbEntities"))
{
using (EntityCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT VALUE a FROM Animals AS a";
conn.Open();
using (EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection))
{
while (reader.Read())
{
// code
}
}

}
}

示例取自此链接 -> http://www.entityframeworktutorial.net/Querying-with-EDM.aspx

编辑。我使用版本 6

public partial class dbEntities : DbContext
{
public dbEntities()
: base("name=dbEntities")
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}

public virtual DbSet<Names> Names { get; set; }
public virtual DbSet<Animals> Animals { get; set; }
}

最佳答案

DbContext实体 SQL 查询中缺少名称。您必须使用 dbEntities.Animals 而不仅仅是 Animals。最后,您的命令文本应如下所示:

SELECT VALUE a FROM dbEntities.Animals AS a

关于c# - 使用 EntityConnection、EntityCommand 和 EntityDataReader 时出现 EntitySqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33720207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com