Click here to Skip to main content
14,697,726 members
Home » Discussions » .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Get list from list of elements link and collections Pin
Richard Deeming13-Dec-19 7:29
mveRichard Deeming13-Dec-19 7:29 
GeneralRe: Get list from list of elements link and collections Pin
simpledeveloper13-Dec-19 9:24
Membersimpledeveloper13-Dec-19 9: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 7:45
Membersimpledeveloper6-Dec-19 7: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 8:29
mveGerry Schmitz6-Dec-19 8: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 8:52
Membersimpledeveloper6-Dec-19 8: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 10:12
Membersimpledeveloper9-Dec-19 10: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 15:40
Membersimpledeveloper9-Dec-19 15: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 10:11
Membersimpledeveloper10-Dec-19 10:11 
Fixed it - the problem is we need to clear up all the objects and its links that the parent object is using then only we can save the changes thanks here is my solution
public bool Delete(NOV nov, bool performCommit = true)
{
    System.Data.Entity.DbContextTransaction dbOperation = null;
    if (performCommit)
        dbOperation = UnitOfWork.BeginTransaction();
    try
    {
        //-- Remove the Items - "foreach" approach was a problem
        // http://weblogs.asp.net/ricardoperes/entity-framework-pitfalls-deleting-orphans

        //------------------------------------------------------
        // Remove the Violations that are in this NOV
        //------------------------------------------------------
        List<Violation> violationIdlist = new List<Violation>();

        foreach (var v in nov.ViolationNOVs)
        {
            var a = UnitOfWork.ViolationRepository.GetAll().Where(z => z.ViolationId == v.ViolationId).FirstOrDefault();
            violationIdlist.Add(a);
        }

        foreach (var v in violationIdlist)
        {
            var a = nov.ViolationNOVs.Where(x => x.NOVId == nov.NOVId && x.ViolationId == v.ViolationId)?.FirstOrDefault();
            nov.ViolationNOVs.Remove(a);
        }

        nov.IssuedBy.Clear();

        //deleting all OneToMany references to NOV
        List<ViolationTypeNOV> novRels = UnitOfWork.Context.ViolationTypeNOVs.Where(x => x.NOVId == nov.NOVId).Include("ViolationType").Include("NOV").ToList();
        nov?.ViolationTypeNOVs?.Clear();
        //foreach (ViolationTypeNOV o in novRels)
        //{
        //    UnitOfWork.Context.ViolationTypeNOVs.Remove(o);
        //    o?.ViolationType?.ViolationTypeNOVs?.Remove(o);
        //    nov?.ViolationTypeNOVs?.Remove(o);
        //}
        UnitOfWork.Context.ViolationTypeNOVs.RemoveRange(novRels);

        List<ViolationNOV> violationNOVs = UnitOfWork.Context.ViolationNOVs.Where(x => x.NOVId == nov.NOVId).Include("Violation").Include("NOV").ToList();
        nov?.ViolationNOVs?.Clear();
        UnitOfWork.Context.ViolationNOVs.RemoveRange(violationNOVs);

        List<CaseNOV> caseNOVs = UnitOfWork.Context.CaseNOVs.Where(x => x.NOVId == nov.NOVId).Include("Case").Include("NOV").ToList();
        nov?.CaseNOVs?.Clear();
        UnitOfWork.Context.CaseNOVs.RemoveRange(caseNOVs);

        UnitOfWork.Context.SaveChanges();
        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;
    }
}

This statement has fixed the problem: UnitOfWork.Context.ViolationTypeNOVs.RemoveRange(novRels); thanks a lot for everybody who tried to help me
QuestionException: Nullable object must have a value while typecasting in a Linq query Pin
simpledeveloper4-Dec-19 11:29
Membersimpledeveloper4-Dec-19 11:29 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
phil.o4-Dec-19 11:51
mvephil.o4-Dec-19 11:51 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
Richard MacCutchan4-Dec-19 23:11
mveRichard MacCutchan4-Dec-19 23:11 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
Richard Deeming5-Dec-19 2:06
mveRichard Deeming5-Dec-19 2:06 
AnswerRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
simpledeveloper5-Dec-19 7:28
Membersimpledeveloper5-Dec-19 7:28 
GeneralRe: Exception: Nullable object must have a value while typecasting in a Linq query Pin
Richard MacCutchan5-Dec-19 8:51
mveRichard MacCutchan5-Dec-19 8:51 
QuestionHow to connect multiple computers to the same database? Pin
SebGM252-Dec-19 15:53
MemberSebGM252-Dec-19 15:53 
AnswerRe: How to connect multiple computers to the same database? Pin
Richard Deeming3-Dec-19 1:41
mveRichard Deeming3-Dec-19 1:41 
GeneralRe: How to connect multiple computers to the same database? Pin
Member 1468812212-Dec-19 21:12
MemberMember 1468812212-Dec-19 21:12 
GeneralRe: How to connect multiple computers to the same database? Pin
amrinde12-Dec-19 21:34
professionalamrinde12-Dec-19 21:34 
GeneralMessage Closed Pin
15-Dec-19 20:12
MemberMember 1468932315-Dec-19 20:12 
AnswerRe: How to connect multiple computers to the same database? Pin
Maciej Los3-Dec-19 2:44
mveMaciej Los3-Dec-19 2:44 
GeneralRe: How to connect multiple computers to the same database? Pin
Richard Deeming3-Dec-19 3:07
mveRichard Deeming3-Dec-19 3:07 
PraiseRe: How to connect multiple computers to the same database? Pin
Maciej Los3-Dec-19 4:19
mveMaciej Los3-Dec-19 4:19 
GeneralOT Pin
Richard Deeming3-Dec-19 4:25
mveRichard Deeming3-Dec-19 4:25 
GeneralRe: OT Pin
Maciej Los3-Dec-19 4:34
mveMaciej Los3-Dec-19 4:34 
PraiseRe: OT Pin
Richard Deeming3-Dec-19 4:38
mveRichard Deeming3-Dec-19 4: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.