I have added a database view to my entity model. Now I am trying to put an ObjectSet
into my ObjectContext
so I can access the view in my application.
For regular tables my ObjectSet
would look like this:
private ObjectSet<StarVendor> _StarVendor;
public ObjectSet<StarVendor> StarVendor
{
get
{
if ((_StarVendor == null))
{
_StarVendor = base.CreateObjectSet<StarVendor>("Stratus_X_TestEntities.StarVendors");
}
return _StarVendor;
}
}
So I did the same for my View:
private ObjectSet<CatalogItemSearch> _CatalogItemSearch;
public ObjectSet<CatalogItemSearch> CatalogItemSearch
{
get
{
if ((_CatalogItemSearch == null))
{
_CatalogItemSearch = base.CreateObjectSet<CatalogItemSearch>("Stratus_X_TestEntities.CatalogItemSearch");
}
return _CatalogItemSearch;
}
}
But when the code runs I get an exception:
System.InvalidOperationException "The EntitySet name 'Stratus_X_TestEntities.CatalogItemSearch' could not be found"
I know that for a View I don't need the add/update/delete functionality that ObjectSet
provides.
Is there an alternative Set type that I should be using for this?
or could this error be from something totally unrelated to the fact that its a view?
thanks
I would like to point out that the ObjectSet
API along with the ObjectContext
API is the old API of the Entity Framework which usually is no longer used today.
It was part of Entity framework 4.0, that was a long time ago (see Wikipedia: Entity Framework history).
In 2011 Entity framework version 4.1 was released, and recommended using the DbSet
and DbContext
API instead from now on.
The current production ready version of entity framwork is version 6, and version 7 is being worked on.
I recommend you to move to Entity framework 6 which you can install into your Visual Studio project using Nuget: EntityFramework 6.1.3
Here is an article which has a chapter about the differences the DbSet
API brings compared to the old API. Take a look at the chapter "Introducing DbContext & DbSet".
If you are using CodeFirst you can always map the view as you map a table (with a DbSet) and then it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With