2
我使用Code First POCO對象在實體框架4.1中創建DataService。當我創建MyEntityContext的實例,並直接訪問它,我可以訪問數據正常,但是當我嘗試通過HTTP它與下面的錯誤打破訪問的DataService:未在代碼中解析的實體先使用Entity Framework 4.1實現DataService
System.ArgumentException: The given name 'Department' was not found in the entity sets.
我已經試過各種事情但不斷回到相同的問題。不知道什麼可能是錯的。
Department是我目前唯一映射的實體。它的定義如下:
[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;
}
}
現在我覺得很蠢!謝謝,這是我需要的一切:) – codemonkey