locked
EF - One to one optional relationship + SQL Server cascade RRS feed

  • Question

  • Hi.

    On SQLServer:

    Table: FinanceiroLancamento

    Id
    Name
    IdFinanceiroLancamentoCaixa
    IdFinanceiroLancamentoBanco

    Table: FinanceiroLancamentoCaixa

    Id
    Money

    On EF - Code First for "FinanceiroLancamento":

                Property(p => p.Name)
                    .IsRequired();

                HasOptional(p => p.FinanceiroLancamentoCaixa)
                    .WithMany()
                    .HasForeignKey(p => p.IdFinanceiroLancamentoCaixa);

    On EF - Code First for "FinanceiroLancamentoCaixa":

                Property(p => p.Money)
                    .HasPrecision(15, 2)
                    .IsRequired();

    My SQLServer relationship:

    ALTER TABLE FinanceiroLancamento ADD CONSTRAINT [FinanceiroLancamento_003_FKEY] FOREIGN KEY ([IdFinanceiroLancamentoCaixa]) REFERENCES FinanceiroLancamentoCaixa([Id]) ON DELETE CASCADE ON UPDATE CASCADE;

    So in the source code I can do:

    var money = financeiroLancamento.FinanceiroLancamentoCaixa.Money:

    The problem in my relationship is that when I delete "FinanceiroLancamento" it´s not deleting "FinanceiroLancamentoCaixa". But if I delete "FinanceiroLancamentoCaixa" it´s delete "FinanceiroLancamento".

    I want the opposite, I would like to delete "FinanceiroLancamento" and it´s delete "FinanceiroLancamentoCaixa". How can I do it?

    (I would like to continue with EF navigating from FinanceiroLancamento to FinanceiroLancamentoCaixa)

    Thanks.

    Best regards,

    Wilton Ruffato Wonrath


    Wilton R. Wonrath

    Thursday, June 6, 2013 1:39 PM

Answers