gpt4 book ai didi

c# - 使用 Entity Framework 4.1 的代码优先 DataService 中未解析实体

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:18 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 4.1 使用 Code First POCO 对象制作数据服务。当我创建 MyEntityContext 的实例并直接访问它时,我可以很好地访问数据,但是当我尝试通过 HTTP 访问 DataService 时,它​​会因以下错误而中断:

System.ArgumentException: The given name 'Department' was not found in the entity sets.

我已经尝试了各种方法,但总是回到同一个问题。不确定哪里出了问题。

部门是我目前映射的唯一实体。定义如下:

[Table("tb_department")]
public class Department
{
[Key]
[Column("department_no", TypeName = "nvarchar")]
public string ID { get; set; }

[Column("department_name", TypeName = "nvarchar")]
public string Name { get; set; }
}

我的数据上下文如下所示:

public class MyEntityContext : DbContext
{
public MyEntityContext(string connStr)
: base(connStr)
{
}

public MyEntityContext()
{
}

public DbSet<Department> Departments { get; set; }
}

我的服务是这样的:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyDataService : DataService<MyEntityContext>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
config.SetEntitySetAccessRule("Department", EntitySetRights.AllRead);
}

protected override MyEntityContext CreateDataSource()
{
MyEntityContext ctx = new MyEntityContext(
Utility.GenerateConnStr
);

return ctx;
}
}

最佳答案

好吧,它说在实体集中找不到“Department”,而您的实体集名称是“Departments”,所以请尝试将调用复数化。

关于c# - 使用 Entity Framework 4.1 的代码优先 DataService 中未解析实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794361/

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