【发布时间】:2015-08-12 15:13:08
【问题描述】:
我有以下 POCO 课程:
public class Client
{
public string ClientId { get; set; }
public string CompanyId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
[ForeignKey("AccountId")]
public virtual ICollection<Note> Notes { get; set; }
}
public class Note
{
public decimal NoteId { get; set; }
public string AccountId { get; set; }
public string NoteValue { get; set; }
[ForeignKey("ClientId")]
public virtual Client Client { get; set; }
}
以及以下映射类:
public ClientMap(string schema)
{
ToTable("CLIENTS", schema);
// Primary Key
HasKey(t => t.ClientId);
// Properties
Property(t => t.ClientId)
.HasColumnName("CLIENT_ID")
.IsRequired()
.HasMaxLength(16);
Property(t => t.CompanyId)
.HasColumnName("COMPANY_ID")
.IsRequired()
.HasMaxLength(16);
Property(t => t.LastName)
.HasColumnName("LAST_NAME")
.IsRequired()
.HasMaxLength(50);
Property(t => t.FirstName)
.HasColumnName("FIRST_NAME")
.IsRequired()
.HasMaxLength(50);
}
public NoteMap(string schema)
{
ToTable("NOTES", schema);
// Primary Key
HasKey(t => t.NoteId);
// Properties
Property(t => t.NoteId)
.HasColumnName("NOTE_ID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.AccountId)
.HasColumnName("ACCOUNT_ID")
.IsRequired()
.HasMaxLength(16);
Property(t => t.NoteValue)
.HasColumnName("NOTE")
.HasMaxLength(4000);
}
在此模型中(使用 Fluent API),客户端和便笺之间存在一对多的关系。 ClientID 是客户端中的 PK,NoteId 是 Notes 中的 PK。数据库中没有外键。 ClientId 映射到 Notes 中的 AccountId。
我无法让它工作。当我运行它时,我可以取回大部分客户端数据,但是在尝试导航到笔记时,我在尝试查看笔记时收到功能评估超时错误。我无法让客户和笔记之间的关系正常工作。我哪里出错了? (我想使用 Fluent API 来实现)
谢谢
【问题讨论】:
标签: c# entity-framework ef-code-first ef-fluent-api