Entity Framework Code First : how to annotate a foreign key for a “Default” value?

前端 未结 4 1049
[愿得一人]
[愿得一人] 2020-12-02 20:32

I have 2 classes: Client and Survey.

Each Client can have many surveys - but only one default survey.

I have defined the classes like this:

p         


        
4条回答
  •  感情败类
    2020-12-02 21:06

    Please notice that adding the code below you will fix the issue.

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext() : base("DefaultConnection")
        {
    
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
              modelBuilder.Entity()
                          .HasOptional(x => x.DefaultSurvey)
                          .WithMany(x => x.Surveys);
                          .HasForeignKey(p => p.DefaultSurveyID);
        {
    }
    

提交回复
热议问题