EF4 is throwing an error "Schema specified is not valid" - entity-framework

I'm getting a weird EF4 "Entity Framework v4" error when I do a select on the context:
Schema specified is not valid. Errors:
The relationship 'AnalyzerConfigurationModel.FK_AnalyzerMetadataParameters_AnalyzerMetadata' was not loaded because the type 'AnalyzerConfigurationModel.AnalyzerMetadataParameter' is not available.
The query to generate the error is:
Using context As New AnalyzerConfigurationEntities
Dim EFAnalyzerConfiguration = (From P In context.AnalyzerConfigurations
Where P.Name = analyzerConfigurationName).FirstOrDefault
End Using
The schema is show below.
I've checked the connection strings, multiple times, its not that. Everything looks fine. I'm not sure if the XML that gets generated from this schema is off or not. But looked there too and don't see anything off or different from other properties. Has anyone run into this one before?

I found that if I expanded the EDMX file in solution explorer (VS 2012) and right clicked on each .tt file and selected Run Custom Tool also fixed the issue. This might be easier than de

Also make sure you're referencing project has the EntityFramework installed otherwise the dependent code will get this error.
https://www.nuget.org/packages/EntityFramework

Let me explain this:
The problem is because the system did not find consistency of the columns in database, the entities in the .edmx file and the model class and also the use of the same in the controller class.
So follow the following steps to fix this up:
Go to the database, check all the column names and properties.
Confirm them with the class diagram in the.edmx file. Right-click on the class in which you are getting the error, and select update option for the class. Select the Refresh tab in dialogue (if there is no new column added. if new column is added, you can select the add tab.) and select the relevant objects. Generally they are tables. So select them and finish.
Next check the names in the model class and the controller class.
This should be the solution. If not, let me know.

Are you using the SelfTracking Entity T4 template? I had this issue and found out the T4 template was not re-generating my entities after I had changed the Entity model in the Entity Designer. Try to right click the T4 Template and click "Run custom tool", and see if that helps.

Related

Entity Framework database first adding new table to exist .edmx

I added a new table to the database. In the .edmx (where database diagram is at), I right-clicked and chose 'Update Model from Database...'. I can see the new table's diagram has been generated, but how do I add model to EXISTING .tt? I know I could do it manually, but I would like to know how to generate it automatically. Thanks.
EDIT: I got the model class by clicking around. I think it was right clicking on .tt then 'Debug T4 template' which generated it. But can anybody explain what did this Debug T4 template debug?

How to generate Views using databasefirst and entity framework?

I've been given a Visual Studio solution to get up and running again for development. The project uses Entity Framework database first. When I generate model from database, the sql wants to convert all of the views into tables. I know views should be avoided with EF, but what is the best way to correct this issue given that the developer no longer works for us?
Thanks
There is nothing wrong with using views with Entity Framework. I assume you mean to say when you update from database it is trying to convert the views to model objects. This is totally fine. often times you are required to specify the primary key for the view. By default Entity Framework will try to use every non null property as the key. in the context menu for each property you can toggle this.
Are you getting an error?
I've come to the conclusion that there is not a way to recreate view entities in your edmx back into actual sql server database views. What needs to happen is that you would generate the sql from the model and run that sql in sql server management studio query analyzer. Delete the tables created that should have been views and figure out what query to write to recreate the views in sql server as they should be. Once that's done, the views in your model should be fine and update there after when running "Update from Database".
Say you have two tables - Categories and Products. You want to create a view called ProductsWithCategoryName. When you do update from database and this view gets added to your EDMX file, viewing the XML shows the following:
<EntitySet Name="ProductsWithCategoryName" EntityType="NorthwindModel.Store.ProductsWithCategoryName" store:Type="Views" store:Schema="dbo" store:Name="ProductsWithCategoryName">
<DefiningQuery>
SELECT
[ProductsWithCategoryName].[ProductID] AS [ProductID],
[ProductsWithCategoryName].[ProductName] AS [ProductName],
[ProductsWithCategoryName].[UnitsInStock] AS [UnitsInStock],
[ProductsWithCategoryName].[CategoryName] AS [CategoryName]
FROM [dbo].[ProductsWithCategoryName] AS [ProductsWithCategoryName]
</DefiningQuery>
</EntitySet>
Problem with the above is that the defining query is NOT the query that creates that view. For you to get the proper defining query you must actually edit the EDMX file by hand adding the proper query to it as follows:
<EntitySet Name="ProductsWithCategoryName" EntityType="NorthwindModel.Store.ProductsWithCategoryName" store:Type="Views" store:Schema="dbo" store:Name="ProductsWithCategoryName">
<DefiningQuery>
SELECT dbo.Products.ProductID, dbo.Products.ProductName, dbo.Products.UnitsInStock, dbo.Categories.CategoryName
FROM dbo.Categories INNER JOIN
dbo.Products ON dbo.Categories.CategoryID = dbo.Products.CategoryID
</DefiningQuery>
</EntitySet>
This still will not give you the expected outcome of a proper sql view getting created. Basically EF when going from the conceptual to the database only produces an table per entity and views are simply seen as another entity.

Can't select the database when updating EF model

I have an edmx model
I right click on it and select Update model from database
The wizard show up .It normally show me Choose Your Data Connexion, but not anymore. Now it show me directly Choose Your Database Objects
How can I select a different database ?
PS: When I create a second edmx in the same project and want to update it, the second one works like it should
After some trials, I found it.
You must delete the connection string in your config file (app.config in my case)

Entity Framework "Invalid object name 'Item_id'"

I have a solution that has spontaneously started generating the error "Invalid object name 'Item_id'". I have searched for this but all answers seem related to code first issues around pluralization; my project is database first.
The context is that there is a Purchase with a collection of ItemPurchaseLines which are linked to Items. The error occurs when a new purchase is generated, this generates new ItemPurchaseLines and either links them to existing Items or generates new Items. The mapping between ItemPurchaseLines and Items is via a foreign key on Item_Id to Id respectively.
I have regenerated the model from the database and the relationships/column mappings all look good.
Any help, or any further information you need, would be appreciated.
Edit
I have built a LinqToSql alternative and it gives exactly the same error.
OK - I am a complete idiot!
I completely forgot that I added a trigger to the relevant table to update the pl_Items table but instead of using the correct table name I used Item_Id.
So the code from VS was all good it was the SQL Trigger in the back and that was screwing things up!

Entity Framework: deleted SQL table is not removed from the model

Seemingly simple thing got me completely frozen and I can't find anything on the Net about this:
I had a common many to many relationship in my db:
Table One + TableTwo + LinkingTable with 2 columns: TableOneID and TableTwoID
I deleted the LinkingTable and tried to update the model from database. Now I get error "Error 11007: Entity type 'LinkingTable' is not mapped."
Does anyone know what exactly this EF wants? Thank you!
Right-click the Model in the Solution Explorer. Select "Open with..." Select "XML (Text) Editor" in the dialog. Remove all nodes and references of the LinkingTable (or whatever its real name is) from the model's xml. Close all model files that are currently opened (I know, weird). Build the project. Open the model again. Everything should be fine now.
Open Model Browser,
In the Entity types, select the table which is to be deleted,
It will remove all the associations with the table.
It worked well for me.