RequiredIf attribute give error when initializing the database the database using EF 5 code first - entity-framework

I am getting the following error when using the RequiredIf attribute in the entity while initializing the database using EF5 code first.
System.Data.DataException: An exception occurred while initializing the database.
See the InnerException for details. ---> System.Data.Entity.Validation.DbEntityValidationException:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
If I comment the [RequiredIf] attribute code first build the database successfully. Any idea?
Update:
I step through the debugger and found that it's adding a validation error. How do I switch this off while initializing the database using code first.

The problem was occurring during the seeding. The requiredIf validation says if the ApplicationStatus is recommended i.e. 2 then the DecisionDate is mandatory. So I added the DecisionDate property to the seed function in my context and it worked. Thank you everybody to looking at the issue anyway.
Lesson learnt is if you put validation in you domain objects then seed data accordingly.

Related

MDS Error when creating a Entity Sync to an New Target Entity

When I try to create an Entity Sync in Master Data Services 2016, I receive the following error (fyi I chose to create a new target entity).
Not sure why this is occurring. Ideas?
It turns out that the Code attribute on the Source entity is the problem as indicated in the error message. It needs to have its display width match its length or you will get this error. See snip below

The model backing the 'XYZ' context has changed since the database was created

I am getting following error
The model backing the 'XYZ' context has changed since the database was created.
How can get to know the Entity / Entities which are casuing issues for logging purposes?
The entity name you are having issue with is 'XYZ' of course, as it it exactly specified in the error you posted.
Although i'm not sure which exception you have to catch, and you should probably not do it, it is clear what you have to do.
Here is some helpful information :
The model backing the <Database> context has changed since the database was created
Also been discused here :
Entity Framework 4.1 The model backing the context has changed since the database was created, immediately after creating DB
Where it is told that catch (PlatformNotSupportedException) may be related. But has i said, this may not be the solution at all.
Finally, the problem may be even deeper, if it's the case, look at this :
The model backing the '--Context' context has changed since the database was created - but db is new production database

Entity framework error on updating model from database and vice versa

Here is what happens when I try to update model from database with VS express 2013 for the web, EF6.1.1 and .NET framework 4.5.
In this case I just added a field to a table in the table definition and updated the database.
After that I right click update model from database in the EDMX model view and I get this error message:
An exception of type ‘System.runtime.interopServices.COMException’
occurred while attempting to update from the database. The exception
message is: ‘A file or folder with the name ‘Model.Context.tt’ already
exists. Please give a unique name to the item you are adding or delete
the existing item first.
I noticed that I get the same error message when I try to generate the database from the model.
I tried the following methods:
How do you update an edmx file with database changes?
http://blog.jongallant.com/2012/08/entity-framework-manual-update.html#.VMYYRv7A7mH
but none of them worked.
I also found that https://entityframework.codeplex.com/workitem/1104 and it seems that it has been resolved with “commit 7e8331d1d22d (EFTools repo)”.
Anyone can help me on how to solve my problem or on what is “commit 7e8331d1d22d (EFTools repo)” ?
Thanks
Boid’
It's an old question but I just run into the same issue. Finally found a solution in this bug report. Check if you have any *.tt files that are not added to solution and remove them. That fixed the problem for me.

Entity Framework 4.0: Adding scaler property gives error (property it not mapped: 11009)?

I wanted to add a new property to one of my model (table). Basically its a property that doesn't exist in the database but i need to add it to my model so that the custom generation tool (self tracking entity generator) will create the the property inside the the custom generated file.
I added a scaler property, its a string and called testme but it gives me the following error, Anybody know how i can fix this?
Error 2538 Error 11009: Property 'testme' is not mapped.
I am confused why do i need to map it to a table... its a field that doesn't exist in the table ...
Any help really appreciated
Thanks
Generally, you add un-mapped properties to a partial class instead of via the model. That said, use discretion; un-mapped properties can be confusing, since they mostly can't be used in LINQ to Entities queries.
I encountered this problem and was able to resolve it by deleting the entity (a view) in tne designer and readding it by refreshing from the database. This occured after a major redesign of the database and rewriting the view.
I know this doesn't address your problem, but Googling for this error returns this question. Hopefully this answer will be useful to others who are new to EF and hit this message, like I did.
I've been generating my DB from my conceptual model. If I modify the model without updating the DB, then I see this error message.
At the moment I don't have any data in my model, so simply regenerating the DB from the changed model makes these errors go away.

Entity Model Issue: Not able to create model for one particular table

While creating the entity model I am getting the following error .
Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
ERROR: Unable to generate model because of the following exception: 'Value does not fall within the expected range.'.
Loading metadata from database took 00:00:02.1203632.
Generating model took 00:00:00.8390920.
Writing out the EDMX file took 00:00:00.
All the tables are getting created but the BC_States table is not getting created.I can't understand why.
I've just been investigating a similar issue. It turns out that Entity Framework outputs additional error information in the Messages section of the Error List window in Visual Studio.
As you can see it's much more useful than the generic error that appears in the Output window. This was crucial in helping me to solve my problem.
The question is not readable but I found the similar question at Ado.Net Entity Data Model Not Updating Correctly.
As for me (I had the same problem) the issue was because of there was no primary key set on one of the tables.