Entity Framework does not generate association property - c#

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.

Related

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

Entity Framework 6 - reuse same table in multiple edmx files

I have been tasked with upgrading our software to use EF6. Previously it was using a combination of EF4 & 5.
We use the database first approach. The upgrade went smoothly as far a code changes go, but after running the application and doing a query the following error is thrown.
Schema specified is not valid. Errors:
The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'tblAccountMaintenance'.
Previously found CLR type 'DALUsers.StatusDB.tblAccountMaintenance', newly found CLR type 'DALUsers.AccountsDB.tblAccountMaintenance'.
The class in questions, tblAccountMaintenance, is generated inside multiple .tt files. The classes are references to the same table, just referenced in different .edmx files.
Simply removing one of the references is not a good option in this case as we have used a similar strategy with several other tables and would require thousands of lines of rewritten code.
What do I need to do to fix this in EF6?
So it turns out the the issue (using the same tables, with the same name in multiple edmx files in the same project) is related to the fact the the .tt files contain new objects that derive from dbContext - and this is a limitation/constraint that is specific to dbContext.
I downloaded this plugin: https://visualstudiogallery.msdn.microsoft.com/66612113-549c-4a9e-a14a-f629ceb3f89a
Which allows me to create EF6 .tt files that derive from EnityObject instead which does not have this constraint. It also makes it so I don't have to update all of my code to use the newer dbContext methods which is a plus.
As a note to others viewing this - this is probably not the best answer if you are starting with a newer project or have a small number of edmx files/tables as EntityObject is not as robust as dbContext, however this is a good band-aid fix - especially if you are like me and going to have to do a complete rewrite when EF7 comes out.

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.

What is the meaning of this Entity Framework exception?

What is the meaning of this exception? Where can I find references of the cause to this kind of exception?
The changes to the database were committed successfully, but an error
occurred while
updating the object context. The ObjectContext might be in an inconsistent state.
Inner exception message: Metadata information for the relationship
'MyModel.FK_T_WORKER_VEHICLE_T_VEHICLE' could not be retrieved. If mapping attributes are used, make sure that the
EdmRelationshipAttribute for the relationship has been defined in the
assembly. When using convention-based mapping, metadata information
for relationships between detached entities cannot be determined.
Parameter name: relationshipName
I am using edmx with code generation set to Default. The pocos are in different project then the edmx file.
In my case, I got same error when I added a new entity(A) to my EDMX diagram which A has Foreign key reference to an existing entity B. Then this error came up when I tried to do CRUD for B.
I did some researches and followed a tip on MetadataException when using Entity Framework Entity Connection which advises to rename metadata in Connection String but it didn't work for me.
Steps to resolve:
The issue was in entity A not B. You need to check newly added entity A to be mapped correctly to your class A in your domain. Check property names and property types to be same (i.e. a column might need to be converted to an Enum type in your diagram to be same with your entity). Keep in mind that property names are case sensitive so if they are in upper-case in your entity it should be in upper-case in your diagram.

Entity Framework 4.1 Disable Model Compatibility Check

I need to use an 'DropCreateDatabaseIfModelChanges' - Initializer class, because I want to create one special entity (table), if it doesn't exist. My problem is, that I've also got another entity in my DbContext, which shouldn't be part of the model compatibility check.
I'm getting the following error message:
Model compatibility cannot be checked because the EdmMetadata type was not inclu
ded in the model. Ensure that IncludeMetadataConvention has been added to the Db
ModelBuilder conventions.
Is there any possibility to exclude a special entity from this check?
EDIT:
I've done what Devart has suggested. The problem seems to be different, than I first tought. It all works fine, if I let EF create a new database with my CheckedContext. But I'm getting the error message above, when I'll try to use my NonCheckedContext wich should use an existing table ...
EDIT2:
This is a working solution. Everything works fine, when the database doesn't exist before. But it's no option for me, to Drop/Create the database.
A possible solution: create a context class inherited from DbContext, and then create two separate subcontexts inherited from the base one - CheckedContext and NonCheckedContext, and set the Database Initialization Strategy accordingly.
Please note that you should access the CheckedContext first so that it fires all its checks.

Categories