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ì?
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ù. –
@Nick Olsen: Đã cập nhật câu trả lời của tôi. – Kamyar