Relacja 1 do 1 code first

0

Cześć chcę miec relacje 1 do 1 w podejsciu code first z tym, ze chce miec mozliwosc dopuszczenia nulla. Czyli mam rekord w tabeli A i nie zawsze mam do niej wpis z tabeli B. Czy poniższy zapis jest poprawny ? Dodam, że jak mam coś takiego zrobione to nie da się migracji zrobić

class A{
 [Key]
 public  int ID{get;set;}
 public  string Opis{get;set;}
}

class B{
[Key, ForeignKey("Reservation")] 
 public int? ID{get;set;}
 public  string Opis2{get;set;}
}
0

Jak chcesz mieć relacje 1:1 to B musi mieć klucz obcy do A i A też musi mieć klucz obcy do B.

0

Niechcący znalazłem takie coś. nie sprawdzałem

Configuring a Required-to-Optional Relationship (One-to–Zero-or-One)
The following example configures a one-to-zero-or-one relationship. The OfficeAssignment has the InstructorID property that is a primary key and a foreign key, because the name of the property does not follow the convention the HasKey method is used to configure the primary key.
// Configure the primary key for the OfficeAssignment
modelBuilder.Entity<OfficeAssignment>()
.HasKey(t => t.InstructorID);

// Map one-to-zero or one relationship
modelBuilder.Entity<OfficeAssignment>()
.HasRequired(t => t.Instructor)
.WithOptional(t => t.OfficeAssignment);

tutaj
https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113).aspx

1 użytkowników online, w tym zalogowanych: 0, gości: 1