EF6 Model First: schema specified is not valid, error 0040 - c#

I see that this question has been asked quite a bit, but none of the solutions have helped me since I'm using model first and not code first.
I have a C# project in VS 2015 using EF6. I am building a database using the model first approach and can successfully generate the SQL code from the model and run it in SSMS. I'm using SQL Server LocalDB.
The problem I have is that whenever I try to add a programmatically created entity to the collection (table) to which it belongs, I always get the error
An exception of type 'System.Data.Entity.Core.MetadataException' occurred in mscorlib.dll but was not handled in user code
Additional information: Schema specified is not valid.
Errors:
Market.ssdl(184,6) : error 0040: The Type nvarchar(max) is not qualified with a namespace or alias. Only primitive types can be used without qualification.
The entity I'm creating is has only one property; a string (or nvarchar(max) in the database). Again, I can create the object, but the moment I try to add it to its collection (or table) before saving any changes, I get the above error. I even tried not naming the Name property, but the error persists.
using (var context = new MarketContainer())
{
// Create data source
var datasource = new DataSource()
{
Name = dataSourceName
};
// Save data source
context.DataSources.Add(datasource);
}
Another SO answer proposed to right click on the .tt file and clicking "Run Custom Tool", but that didn't do anything.
I tried this once with MySQL and it worked fine! Now that I need to move to SQL Server it doesn't work... I've been stuck on this problem for over a week, so really any help would go a long way.

The issue has been resolved. I was referencing the project which contained the entity model from another project, and the App.configs were not matching. Once I copied the contents from the entity project's config file to the referencing project's, everything began working properly.
Hopefully this helps someone other than myself!

Related

EntityFramework Schema specified is not valid

I know this question was already asked but it seams that my case might be slightly different. I tried the "run custom tool" but here's where the strange thing happens: Because i'm having 2 related databases (so 2 related models). If i'm running the custom tool on one model it screws up the other and vice-versa(incomplete .cs files, missing, etc.). Does anyone have any ideea where i'm going wrong?
EDIT:
the complete error:
An exception of type 'System.Data.Entity.Core.MetadataException'
occurred in EntityFramework.dll but was not handled in user code
Additional information: Schema specified is not valid. Errors:
The relationship
'ProductionMasterDataEntityModel.FK_ProductGroup_CostPeriods' was not
loaded because the type 'ProductionMasterDataEntityModel.ProductGroup'
is not available.
The following information may be useful in resolving the previous
error:
The required property 'CstAveOrderQty' does not exist on the type
'SISCOM.Persistance.Models.ProductGroup'.
The custom tool is: TextTemplatingFileGenerator
I'm not sure if it has anything to do with the asp.net framework but it is an asp.net project so i thought it's worth mentioning.
in my case, this Issue came after I update .edmx file. Remove all the tables on it and re-update it.right click on .tt file(Eg. Entity.tt) file->Run custom tools. then my issue solved.
I solve this case like below.
Entity in class lib project and i was try to consume in other project.
So in that project i have add reference "EntityFramework.SqlServer.dll" in new project.
Drop the model from edmx and add it again should sort it out. Note this cannot be reverted unless using source control tools.
It seams that i had 2 problems contributing to that error.. First, there were some stored procedures and views, after i got that out of the way (deleted my models, created them again and i've unchecked the option to include the stored procedures and i only added the tables to the model, no views and no stored procedures) i could access some data but not all of it.. The second problem was that i forgot to add a connection to the dependent database.
So the methods in my repository look like this:
public myType GetSomething()
{
var db = new model();
var dependencyDb = new dependencyModel();
//do whatever needs to be done with the data before presenting it
return something;
}
P.S... i didn't had to explicitly take it to the needed table, after i added the connection it found all that it needed by itself.
Hope this helps anyone having this problem.

Create database structure using code first entity framework

I am coding a C# MVC 5 internet application and I have changed the context class. I wish to create a new database with the changed context class as I am getting the following error:
Invalid object name 'dbo.MapLocations'.
I have renamed the database name in the web.config, compiled and run the application, yet am still getting the above error.
How can I recreate the database to have the structure of my context class such that I do not get the above error? I thought that specifying a new database name will automatically create a new database using the entity framework.
I have no important data in the database.
Thanks in advance
EDIT
I have manually deleted the files in the App_Data folder for the project, enabled migrations, added a migration and then updated the database.
This is the error that I am getting in the package manager console:
Cannot attach the file 'C:\Users\**path**\App_Data\CanFindLocation.Context.DataContext.mdf' as database 'CanFindLocation.Context.DataContext'.
Try to use DropCreateDatabaseAlways class. Similar quetion here.
Be carefull, it always drops and recreates your database.

Entity Framework does not generate association property

So.
I'm having trouble fixing this little problem.
I have several classes, and it's all nice and good. Right up until now. I have now added another class (MatchResult), and it works.
But when I try to make the correct association:
It fails with a runtime exception.
Error:
Schema specified is not valid. Errors:
The relationship 'DbModel.FK_ProductPrice' was not loaded
because the type 'DbModel.Product' is not available.
The following information may be useful in resolving the previous
error:
The required property 'MatchResults' does not exist on the type
'PriceMonitor.Model.Product'.
The relationship 'DbModel.FK_WebshopProduct' was not loaded
because the type 'DbModel.Product' is not available.
The following information may be useful in resolving the previous
error:
The required property 'MatchResults' does not exist on the type
'PriceMonitor.Model.Product'.
It seems - for some reason - that EF does not create the MatchResults property on the Product class.
This approach has worked on every single class I've ever made using EF. Up until now.
I found the answer. Apparently, it is a confirmed bug in the Entity Framework. (WHAT?!)
It seems, if you place the .emdx file in a subfolder, automatic code generation does not work.
See this question.
I just had the same error, the name of my csdl, ssdl and msl were not the same als the name of my edmx and contained the name of a other edmx I had within my solution. I changed the connectionstring and fixed it.
This happens when using EF Database First and the generated POCO classes are not up to date with the edmx file, for example when POCO classes are in a different project than edmx.

Invalid column name in my web application

I have urgent and unexplainable problem so any help would be appreciated. I haave 2 different databases which are exactly the same except there is different data in each of them.
I have a web application using LINQ-To-EF and until I've changed the database in connection string everything was working fine. Even though the databases are exactly the same I receive the error: "Invalid column name 'tema_id'." The problem is that "tema_id" doesn't exist in any of those two databases, however, somehow it does exist in .edmx file. The name of the mapping should be "aktivnost_id" and not "tema_id" how it is now.
I've tried updating the model from the database, but in that case everything gets wrong and I get dozens of different errors in Error List.
I've provided the screenshot of mapping details for the problematic table (you can see "tema_id" which should be "aktivnost_id").
I know my explanation might be a bit confusing, but if any additional info is needed I will provide it.
It I hard to give a complete answer without the full details of the errors that occur when you try to update; however, I would be sorely tempted to edit the EDMX as XML, use "find" to locate tema_id, and fix directly.
If nothing else, it is quick to try :)
Have you tried to edit the .EDMX file directly to match the actual table structure?
Try to generate whole data access layer manually outside of visual studio through edmgen.exe
use the following command for EF4 (adjust parameters to reflect your db name, username, password)
#"%windir%\Microsoft.NET\Framework\v4.0.30319\edmgen.exe" /mode:fullgeneration /c:"Data Source=tcp:127.0.0.1;Initial Catalog=your_database;User ID=sa;Password=your_password;Integrated Security=False;" /project:DataContext /entitycontainer:DataContext /namespace:Project /language:CSharp /pluralize

Problem with Entity Framework after adding column to DB table

I needed to add a new NVARCHAR column to a table in my DB. So I added the column, and then fired up Visual Studio to update the EDMX-file for the Entity Framework.
I ran update model from database on everything, which only resulted in "data reader is incompatible"-errors. So I renamed the whole table in the DB, updated EDMX from database, renamed the table back to the original name, ran update again, and then created new function imports for all affected stored procedures. But I still get the same error:
The data reader is incompatible with
the specified '[Model].[Entity]'. A
member of the type,
'[Column]', does not have a
corresponding column in the data
reader with the same name.
I've looked around a bit, and this seems to be a common error if the column name is different in the database and framework. This is however not the case, they have the same name.
I can access the column in the code via [Entity].Context.[Column], so I don't quite see what the data reader is complaining about.
I've run out of ideas, so any help welcome.
Updating the model replaces the store schema, but not the client schema or the mapping. To start with "a clean slate", back up your current EDMX, then open it as XML. Remove all references to the table, then close and open in the graphical error. Build. If you have any errors (perhaps broken links to the deleted table), fix them. Then update model to re-add the table.
Just had the same issues as above. Tried removing the entity from the edmx, tried removing and re-adding the function import, and finally rebuilt using a complex type in the function import and that didn't work either.
I realize this error can occur for really any scenario where there is a mismatch but we found the issue was in the stored proc we used for the function import. The stored proc was using specific select columnname, columnname etc and the new column we added to the table was not in that list. To test, we used the *, updated the EDMX and it resolved our issue.
Turns out the EDMX was fine, but the designer has, for some odd reason, stopped update Designer.cs in my project.
Had to go in and manually edit it.
For me it was an issue where I altered an entity (vertical entity splitting), but one of my stored procedures wasn't pulling data for that new field. I added it (in the SP) and now everything runs fine.
Unless it's simply adding a new item, This throws an error just about every time I try to "Update model from database" ...
The solution is luckily very simple -
1) Open Web.config, find <connectionStrings> node
2) Delete the DBNameEntities <connectionStrings>
- this way you do not have to modify any references to your emdx model
2) Delete the ADO.NET Entity Data Model (.emdx)
3) Re-add the ADO.NET Entity Data Model (.emdx) with the same name.
Its faster and bulletproof. (so far!?)
If you Re-Add the DataModel you will lose all DataBindings - especially bindings of controls on your form (like DbGrid). I have fixed this issue manually editing the DataModel.edmx file in external Editor.

Categories