Reputation: 973
I have generated an ADO.NET data model from my database in Visual Studio 2015, the generated class in myEntities.Context.cs
is named myEntities
and inherits from DbContext
.
I want to extract objects data via LINQ from the model in code behind in MyPage.cs
.
From my book and tutorials, I can see that to make this, you make an instance of the model class, in my case myEntities
and run the LINQ query over it.
However, when I try to instance this class it is not found. The class is public
in the namespace MyAppName.App_Code
and MyPage.cs
is in the namespace MyAppName
.
I can't seem to include the MyAppName.App_Code
namespace in my code behind for MyPage.cs
, Visual Studio tells me that it's redundant and the code behind still doesn't see or accept the class myEntities
.
Edit: More clear: In MyEntities.Context.cs in App_Code folder
namespace MyApp.App_Code
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MyEntities : DbContext
{
public MyEntities()
: base("name=MyEntities")
{
}
}}
Code behind of MyPage.cs
namespace MyApp
{
public partial class _default : System.Web.UI.Page
{
public IQueryable ListView_GetData()
{
//can't use the MyEntities class here, tried to add the
//MyApp.App_code namespace above, still did't work
var data = MyEntities();
//my LINQ operations
return null;
}
}}
Upvotes: 0
Views: 1450
Reputation: 6969
From Microsoft documentation:
Code in the App_Code folder is referenced automatically in your application.
Putting your DbContext
class in App_Code
means it will be automatically available anywhere in your application without an explicit reference. So you would be able to write:
var exampleTableEntities = MyEntities.ExampleTableEntities;
If you want an explicit reference, I would suggest moving your DbContext
outside the App_Code
folder into a folder named Data
, Domain
or something similar. Then reference from your code file.
Upvotes: 1