Click here to Skip to main content
15,892,005 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionGet list from list of elements link and collections Pin
simpledeveloper13-Dec-19 6:13
simpledeveloper13-Dec-19 6:13 
AnswerRe: Get list from list of elements link and collections Pin
Richard Deeming13-Dec-19 6:29
mveRichard Deeming13-Dec-19 6:29 
GeneralRe: Get list from list of elements link and collections Pin
simpledeveloper13-Dec-19 8:24
simpledeveloper13-Dec-19 8:24 
QuestionThe operation failed: The relationship could not be changed because one or more of the foreign-key - EF Code first Pin
simpledeveloper6-Dec-19 6:45
simpledeveloper6-Dec-19 6:45 
AnswerRe: The operation failed: The relationship could not be changed because one or more of the foreign-key - EF Code first Pin
Gerry Schmitz6-Dec-19 7:29
mveGerry Schmitz6-Dec-19 7:29 
GeneralRe: The operation failed: The relationship could not be changed because one or more of the foreign-key - EF Code first Pin
simpledeveloper6-Dec-19 7:52
simpledeveloper6-Dec-19 7:52 
GeneralRe: The operation failed: The relationship could not be changed because one or more of the foreign-key - EF Code first Pin
simpledeveloper9-Dec-19 9:12
simpledeveloper9-Dec-19 9:12 
GeneralRe: The operation failed: The relationship could not be changed because one or more of the foreign-key - EF Code first Pin
simpledeveloper9-Dec-19 14:40
simpledeveloper9-Dec-19 14:40 
I have a simple association Table - whose PK isn't referenced anywhere but when I am trying to delete a record from it in the following way - I am getting an Error - I am using EF Code First - any help would be very very helpful - thanks in advance.
List<ViolationTypeNOV> novRels = UnitOfWork.Context.ViolationTypeNOVs.Where(x => x.NOVId == nov.NOVId).Include("ViolationType").Include("NOV").ToList();
                    
    foreach (ViolationTypeNOV o in novRels)
    {
         UnitOfWork.Context.ViolationTypeNOVs.Remove(o);
    }                
    UnitOfWork.Context.SaveChanges();

Here is the error message I am getting - if the Tables PK isn't referenced in any way - why is it failing with this error just not able to understand, I am using EF Code First - thank you.

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

The same thing if I am running through SSMS same thing is working though:
Delete ViolationTypeNOVs where ViolationTypeNOVId = 2
Why?
But again if I am running the same query through context as below, its giving me the same error at the calling of SaveChanges:
foreach (ViolationTypeNOV o in novRels)
                    {
                        string str = string.Format("Delete ViolationTypeNOVs where ViolationTypeNOVId = {0}", new object[] { o.ViolationTypeNOVId });
UnitOfWork.Context.Database.ExecuteSqlCommand(str);
                    }
    UnitOfWork.Context.SaveChanges();


It seems like some of the objects within the context aren't nulling or getting deleted, is there anyway to clear them all in one go? Because these ids/objects are used in multiple places in the code - please let me know how to clear them all - thanks a lot.

Any help please?

modified 10-Dec-19 10:06am.

AnswerRe: The operation failed: The relationship could not be changed because one or more of the foreign-key - EF Code first Pin
simpledeveloper10-Dec-19 9:11
simpledeveloper10-Dec-19 9:11 
QuestionException: Nullable object must have a value while typecasting in a Linq query Pin
simpledeveloper4-Dec-19 10:29
simpledeveloper4-Dec-19 10:29 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
phil.o4-Dec-19 10:51
professionalphil.o4-Dec-19 10:51 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
Richard MacCutchan4-Dec-19 22:11
mveRichard MacCutchan4-Dec-19 22:11 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
Richard Deeming5-Dec-19 1:06
mveRichard Deeming5-Dec-19 1:06 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
simpledeveloper5-Dec-19 6:28
simpledeveloper5-Dec-19 6:28 
GeneralRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
Richard MacCutchan5-Dec-19 7:51
mveRichard MacCutchan5-Dec-19 7:51 
QuestionHow to connect multiple computers to the same database? Pin
SebGM252-Dec-19 14:53
SebGM252-Dec-19 14:53 
AnswerRe: How to connect multiple computers to the same database? Pin
Richard Deeming3-Dec-19 0:41
mveRichard Deeming3-Dec-19 0:41 
GeneralRe: How to connect multiple computers to the same database? Pin
Member 1468812212-Dec-19 20:12
Member 1468812212-Dec-19 20:12 
GeneralRe: How to connect multiple computers to the same database? Pin
amrinde12-Dec-19 20:34
professionalamrinde12-Dec-19 20:34 
AnswerRe: How to connect multiple computers to the same database? Pin
Maciej Los3-Dec-19 1:44
mveMaciej Los3-Dec-19 1:44 
GeneralRe: How to connect multiple computers to the same database? Pin
Richard Deeming3-Dec-19 2:07
mveRichard Deeming3-Dec-19 2:07 
PraiseRe: How to connect multiple computers to the same database? Pin
Maciej Los3-Dec-19 3:19
mveMaciej Los3-Dec-19 3:19 
GeneralOT Pin
Richard Deeming3-Dec-19 3:25
mveRichard Deeming3-Dec-19 3:25 
GeneralRe: OT Pin
Maciej Los3-Dec-19 3:34
mveMaciej Los3-Dec-19 3:34 
PraiseRe: OT Pin
Richard Deeming3-Dec-19 3:38
mveRichard Deeming3-Dec-19 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.