Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:03:38+00:00 2026-06-18T05:03:38+00:00

I am using Entity Framework code first. I was trying to put 2 foreign

  • 0

I am using Entity Framework code first.

I was trying to put 2 foreign keys on the same table but I am not being allowed to do so.

It’s like that(edited) :

The table is called Avaliacao:

[Required]
public int AvaliacaoId { get; set; }


[Required]
public int LivroId { get; set; }
public int? AutorId { get; set; } 

public virtual Livro Livro { get; set; }
public virtual Autor Autor { get; set; }

I wanted AutorId not to be null but it only works that way.

I wish I could have 2 non-nullable FK but only one Delete on Cascade.

How do I achieve this with Entity Framework code-first?

Somebody help me please

thx in advance

ZeCarioca

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:03:40+00:00Added an answer on June 18, 2026 at 5:03 am

    EDIT:

    I have not tested this, but if you are using EF5 you could make use of the OnModelCreating method by overiding it in your DbContext. You can call the same entity multiple times to add configuration so you could specify a second foreign key, set its HasRequired property and set its WillCascadeOnDelete property to true.

    Something like this for the first foreign key.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Avaliacao>()
                    .HasRequired(a => a.LivroId)
                    .HasForeignKey(m => a.LivroId)
                    .WillCascadeOnDelete(false);
    
        modelBuilder.Entity<Avaliacao>()
                    .HasRequired(a => a.AutorId)
                    .HasForeignKey(m => a.AutorId)
                    .WillCascadeOnDelete(false);
    
    }
    

    For more reference on the method you can look here at the MSDN Docs: DbModelBuilder

    As mentioned I have not tested this myself so you might need to change some of the properties.

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using Entity Framework Code First with Foreign Key relationships. We investigating on
I am trying to update my entity using Entity Framework 4.1 code First approach
I'm using Entity Framework 4.3 with code first and manual migrations. I'm trying to
I am using Entity framework Code First CTP5 and I am trying to find
We are using Entity Framework Code First, and I am running into issues trying
I'm using Entity Framework 4.1 Code First and have been trying to add a
I am trying to use MVC3 with the Entity framework with Code First, but
I'm trying to map an existing database using Entity Framework Code First Fluent API.
I am using Entity Framework 4.1 code first. I had a table in the
I'm trying to implement the repository pattern using entity framework code first rc 1.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.