1

First of all I saw Implementing Zero Or One to Zero Or One relationship in EF Code first by Fluent API.

I want implement 0..1(Student):0..1(GraduationWork) in my MVC 4 project.

I have POCO classes:

public class GraduationWork
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int GraduationWorkID { get; set; }
    [Display(Name = "Student")]
    public int? StudentID { get; set; }
    [ForeignKey("StudentID")]
    public virtual Student Student { get; set; }
    ....
}

and

public class Student : UserProfile
{
    [Required]
    public int? GraduationWorkID { get; set; }

    [ForeignKey("GraduationWorkID")]
    public virtual GraduationWork GraduationWork { get; set; }
    ....
}

and there is part of my OnModelCreating:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>().HasOptional(x => x.GraduationWork)
        .WithOptionalPrincipal(s => s.Student)
        .Map(x => x.MapKey("StudentID"));

        modelBuilder.Entity<GraduationWork>().HasOptional(x => x.Student)
        .WithOptionalPrincipal(g => g.GraduationWork)
        .Map(x => x.MapKey("GraduationWorkID"));
    }

I think maybe is there problem with inheritance, but I don´t know... Would you be so kind and help me with this? :)

Community
  • 1
  • 1

1 Answers1

1

According to this Link

You can create 0.1---0.1 mapping in 2 ways.

  1. Using Attributes
  2. Using Fluent Api

**************************************Using Attributes********************

public class Student : UserProfile
{
    public int StudentId { get; set; }
    public virtual GraduationWork GraduationWork { get; set; }       
}

public class GraduationWork
{
    [Key]
    [ForeignKey("Student")]        
    public int StudentID { get; set; }       
    public virtual Student Student { get; set; }

}

Note: There's a one-to-zero-or-one relationship between the Student and the GraduationWork entities. An GraduationWork only exists in relation to the Student it's assigned to, and therefore its primary key is also its foreign key to the Student entity. But the Entity Framework can't automatically recognize StudentID as the primary key in GraduationWork Entity because its name doesn't follow the ID or classnameID naming convention. Therefore, the Key attribute is used to identify it as the key:

[Key]
[ForeignKey("Student")]
public int StudentID { get; set; }

*******************************Using Fluent Api***********************

The following code provides an example of how you could have used fluent API instead of attributes to specify the relationship between the Student and GraduationWork entities:

modelBuilder.Entity<Student>()
    .HasOptional(p => p.GraduationWork ).WithRequired(p => p.Student);

for more information

Sql define one to one relationship

  1. Defining a one-to-one relationship in SQL Server

  2. How to create one to one relationship SQL server diagram

Regards

Shaz

Community
  • 1
  • 1
Shazhad Ilyas
  • 1,183
  • 8
  • 14
  • Thanks Shaz, but I don't know how to do it. I tried multiple possibilities, but it's not done. It says for example: "Unable to determine the principal end of an association between the types '....Models.GraduationWork' and '...Models.Student'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.". I know that it has to be configured, but I dont know how. As I said, I tried multiple possibilities. Would you be so kind and help me little more, plese? – Ondřej Daněk Mar 31 '14 at 08:38
  • did you migrate your database before applying new changes..hows your tables looks in DB..?? – Shazhad Ilyas Mar 31 '14 at 08:46
  • Exception from first post is thrown when im trying to add-migration – Ondřej Daněk Mar 31 '14 at 10:36
  • because ef cannot covert the entities structure into real database structure in sql ..please read the defining one to one realship in sql – Shazhad Ilyas Mar 31 '14 at 10:42