EF 4.1 Bidirectional one-to-one problem

Hi I have a problem with a simple EF 4.1 code model.

I have a class and a cool study that are bidirectionally connected. The database model is correct, but I always get this error:

Unable to determine the principal end of an association between the types 'DAL.Models.Survey' and 'DAL.Models.Person'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Face class

[Key]
        public Guid Id { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string Email { get; set; }

        public virtual Survey Survey { get; set; }

Class overview

   [Key]
        public Guid Id { get; set; }

        public bool IsFinished { get; set; }

        public virtual Person Person { get; set; }

DataContext:

 modelBuilder.Entity<Survey>().HasRequired(s => s.Person).WithOptional().WillCascadeOnDelete(true);

Can anybody help

+3
source share
2 answers

You must define another navigation property in your mapping, as it is in the model. Otherwise, EF will create a second association (one to many):

modelBuilder.Entity<Survey>()
            .HasRequired(s => s.Person)
            .WithOptional(p => p.Survey)
            .WillCascadeOnDelete(true);
+1
source

I think you need to specify the foreign key property through the HasForeignKey column name or the foreign key name using Map. Something like that:

modelBuilder.Entity<Survey>()
    .HasRequired(s => s.Person)
    .WithOptional()
    .WillCascadeOnDelete(true)
    .Map(m => m.MapKey("fk_column"));
0
source

All Articles