2011-10-07 11 views
9

Mam następujące dwa modeleEntity Code Framework Pierwszy One to One związku

public class Account 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int CurrentPeriodId { get; set; } 

    [ForeignKey("CurrentPeriodId")] 
    public virtual Period CurrentPeriod { get; set; } 

    public virtual ICollection<Period> Periods { get; set; } 
} 

public class Period 
{ 
    public int Id { get; set; } 
    public int AccountId { get; set; } 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 

    public virtual Account Account { get; set; } 
} 

i próbuję uruchomić następujące zapytanie:

from p in context.Periods 
where p.AccountId == accountId && 
     p.Id == p.Account.CurrentPeriodId 
select p.StartDate 

I uzyskać sql wyjątek mówiący „Nieprawidłowy nazwa kolumny Account_AccountId ".

wiem mogłem podejść do tego z boku konto i zrobić coś podobnego

from a in context.Accounts 
where a.Id == id 
select a.CurrentPeriod.StartDate 

Ale chciałbym wiedzieć, jak skonfigurować relacji, aby uzyskać drugi zapytanie do pracy. czego mi brakuje?

Odpowiedz

8

Myślę, że twoje wdrożenie jest złe. AFAIK, nie możesz zdefiniować relacji jeden-do-jednego używając tego podejścia w EF.
Spójrz na wygenerowaną bazę danych, masz kolumnę Account_Id zdefiniowaną w tabeli Periods. Oto, co trzeba określić:

public class Account 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int CurrentPeriodId { get; set; } 
    public virtual Period CurrentPeriod { get; set; } 
    public virtual ICollection<Period> Periods { get; set; } 
} 

public class Period 
{ 
    public int Id { get; set; } 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 
} 

Następnie kontekst i inicjator:

public class TestContext : DbContext 
{ 
    public DbSet<Account> Accounts { get; set; } 
    public DbSet<Period> Periods { get; set; } 

static TestContext() 
{ 
    Database.SetInitializer(new DbInitializer()); 
} 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Account>().HasRequired(a => a.CurrentPeriod).WithMany().HasForeignKey(a => a.CurrentPeriodId); 
    } 
} 

class DbInitializer : DropCreateDatabaseAlways<TestContext> 
{ 
    protected override void Seed(TestContext context) 
    { 
     context.Database.ExecuteSqlCommand("ALTER TABLE Accounts ADD CONSTRAINT uc_Period UNIQUE(CurrentPeriodId)"); 
    } 
} 

Aby uzyskać więcej informacji na temat One-to-One związek, czytać bloga serię pana Manavi pod adresem this address.
Aktualizacja:
podstawie Twojego komentarza, jeśli chcesz mieć odniesienie do Account z Period, można umieścić atrybut ForeignKey na klucz podstawowy na koncie.

Dobrym próbka znajduje się w this address (część z tytułem „Mapa jeden do zero lub jeden związek”)

+0

Dzięki takiemu rozwiązaniu zgubiłem odniesienie do konta z Okresu chociaż. –

+0

@Nick Olsen: Zaktualizowano moją odpowiedź. – Kamyar

Powiązane problemy