Search code examples
c#sql-serverentity-frameworklocaldbef-model-first

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


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.


Solution

  • 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!