2011-10-07 38 views
9

Tôi có hai mô hình sauEntity Framework Mã Đầu One to One Mối quan hệ

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; } 
} 

Và tôi đang cố gắng để chạy các truy vấn sau đây:

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

Và tôi nhận được một ngoại lệ sql nói "không hợp lệ tên cột Account_AccountId ".

Tôi biết tôi có thể tiếp cận này từ phía tài khoản và làm điều gì đó như

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

Nhưng tôi muốn biết làm thế nào để thiết lập mối quan hệ để có được những truy vấn khác để làm việc. Tôi đang thiếu gì?

Trả lời

8

Tôi nghĩ việc triển khai của bạn là sai. AFAIK, Bạn không thể xác định mối quan hệ một-một bằng cách sử dụng phương pháp này trong EF.
Hãy xem cơ sở dữ liệu đã tạo của bạn, bạn có cột Account_Id được xác định trong bảng Periods của mình. Dưới đây là những gì bạn phải xác định:

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; } 
} 

Sau đó, bối cảnh và khởi tạo:

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)"); 
    } 
} 

Để biết thêm thông tin về One-To-One mối quan hệ, đọc loạt blog của ông Manavi tại this address.
Cập nhật:
Dựa trên nhận xét của bạn, nếu bạn muốn có một tham chiếu đến Account từ Period, Bạn có thể đặt một thuộc tính ForeignKey vào khóa chính của tài khoản của bạn.

Một mẫu tốt tọa lạc tại this address (phần với tiêu đề "Bản đồ One to Zero hay Một mối quan hệ")

+0

Với giải pháp này, tôi đã bị mất tài liệu tham khảo của tôi vào Tài khoản từ thời kỳ mặc dù. –

+0

@Nick Olsen: Đã cập nhật câu trả lời của tôi. – Kamyar

Các vấn đề liên quan