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

.NET (Core and Framework)

 
QuestionRichTextBox Viewer Pin
Diex196819-Dec-19 4:25
Diex196819-Dec-19 4:25 
AnswerRe: RichTextBox Viewer Pin
Eddy Vluggen21-Dec-19 10:41
professionalEddy Vluggen21-Dec-19 10:41 
GeneralRe: RichTextBox Viewer Pin
Gerry Schmitz22-Dec-19 3:24
mveGerry Schmitz22-Dec-19 3:24 
GeneralRe: RichTextBox Viewer Pin
Diex196822-Dec-19 7:08
Diex196822-Dec-19 7:08 
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 
I am using Entity Framework Code First - I am getting the following message in deleting any of the tables data - like CaseNov, ViolationsNov and ViolationTypeNov are three tables which are to handle many to many relationships between Case-Nov, Violation-Nov and ViolationType-Nov, I am getting error messages even if I trying to delete the detailed tables like: CaseNov, ViolationsNov, ViolationTypeNov or row directly from NOV table, I am getting the similar type of message - any help please? I am using Entity Framework Code First for deleting it.
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.

Here is the code for deleting records any help please:
public bool Delete(NOV nov, bool performCommit = true)
{
    System.Data.Entity.DbContextTransaction dbOperation = null;
    if (performCommit)
        dbOperation = UnitOfWork.BeginTransaction();
    try
    {
        //deleting all OneToMany references to NOV
        List<ViolationTypeNOV> novRels = UnitOfWork.ViolationTypeNOVRepository
            .GetAll().Where(x => x.NOVId == nov.NOVId).ToList();

        foreach (ViolationTypeNOV o in novRels)
        {
            nov.ViolationTypeNOVs.Remove(o);
            this.UnitOfWork.ViolationTypeNOVRepository.Delete(o.ViolationTypeNOVId);
        }
        novRels.RemoveAll(x => x.NOVId == nov.NOVId);

        List<ViolationNOV> violationNOVs = UnitOfWork.ViolationNOVRepository
            .GetAll().Where(x => x.NOVId == nov.NOVId).ToList();
        foreach (ViolationNOV o in violationNOVs)
        {
            nov.ViolationNOVs.Remove(o);
            this.UnitOfWork.ViolationNOVRepository.Delete(o.ViolationNOVId);
        }
        violationNOVs.RemoveAll(x => x.NOVId == nov.NOVId);

        List<CaseNOV> caseNOVs = UnitOfWork.CaseNOVRepository
            .GetAll().Where(x => x.NOVId == nov.NOVId).ToList();
        foreach (CaseNOV o in caseNOVs)
        {
            nov.CaseNOVs.Remove(o);
            this.UnitOfWork.CaseNOVRepository.Delete(o.CaseNOVId);
        }
        caseNOVs.RemoveAll(x => x.NOVId == nov.NOVId);

        UnitOfWork.NOVRepository.Delete(nov.NOVId);

        if (dbOperation != null)
            dbOperation.Commit();

        LogHandler.LogInfo(2521, "Deleted NOV " + nov.NOVNumber);
        return true;
    }
    catch (Exception ex)
    {
        LogHandler.LogError(2523, "Commit Fail in NOV Delete", ex);
        if (dbOperation != null)
            dbOperation.Rollback();
        throw ex;
    }
}


modified 6-Dec-19 12:54pm.

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

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.