Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Foreign Key using Data Annotations

In the code below, I need to set a foreign key constrant on ParentInfoAddProperties.ParentQuestionAnswersId so that it is dependent on ParentQuestionAnswers.Id (which is a Primary Key). I am attempting to do so using data annotations but Entity Framework 6 wants to create a new Foreign Key column in my ParentQuestionAnswers table which references the ParentInfoAddProperties.Id column not the ParentInfoAddProperties.ParentQuestionAnswersId column. I do not want Entity Framework to create a new foreign key column.

I'd greatly appreciate if someone can explain what data annotations or (if necessary) fluent mappings I should specify to achieve the desired foreign key constrant. Thanks in advance.

namespace Project.Domain.Entities
{  
    public class ParentQuestionAnswers
    {
        public ParentQuestionAnswers()
        {
            ParentInfoAddProperties = new ParentInfoAddProperties();
        }

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

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

        public ParentInfoAddProperties ParentInfoAddProperties { get; set; }
    }

    public class ParentInfoAddProperties
    {
        [Required]
        public int Id { get; set; }

        [Required]
        public int ParentQuestionAnswersId { get; set; }
    }
}
like image 606
gemini6609 Avatar asked Jun 02 '14 18:06

gemini6609


People also ask

How do you use foreign key data annotations?

The [ForeignKey(name)] attribute can be applied in three ways: [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.

How do you set a foreign key in EF?

If you want the foreign key to reference a property other than the primary key, you can use the Fluent API to configure the principal key property for the relationship. The property that you configure as the principal key will automatically be set up as an alternate key.

How do you create a foreign key for a model?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table. Thanks for reading this article, hope you enjoyed it.

What is foreign key in C#?

Enforces database integrity. A foreign key in one table points to primary key in another table. The foreign key prevents invalid data from being inserted into the foreign key column. The values that you enter in the foreign key column have to be one of the values contained in the table that it points to.


1 Answers

You could use the following data annotation, and use entity instead of int

[Required]
[ForeignKey("ParentQuestionAnswers")]
public ParentQuestionAnswers ParentQuestionAnswers { get; set; }

to get the Id only you could add the property

public int ParentQuestionAnswersId { get; set; }

but you still need the ParentQuestionAnswers property so EF will understand you .

(these code rows should be under the ParentInfoAddProperties class)

like image 190
jony89 Avatar answered Oct 01 '22 03:10

jony89