EF Code First Fluent API,指定Foreign Key属性 [英] EF Code First Fluent API specifying the Foreign Key property
问题描述
public class AgentBalance
{
...
public int AgentId {get;组; }
public virtual Agent Agent {get;组; }
}
AgentId被检测为代理关系的FK约定,但我想在Mapping类中明确表示,以便对未来的更改更安全。如果代理人收集了余额,那么我知道如何做到这一点,例如:
HasRequired(t => t.Agent) .WithMany(a => a.Balances).HasForeignKey(t => t.AgentId);
然而,代理不有一个余额集 - 我不不想让这个关联反向导航。但是没有.WithMany在映射中我没有得到指定.HasForeignKey的选项。还有另一种方式吗? (NB我知道我也可以使用属性,但是我想使用流畅的API映射)。
我相信你应该可以这样做:
HasRequired(t => t.Agent).WithMany()。HasForeignKey(t => t.AgentId)
I have a class AgentBalance with an association to Agent, thus:
public class AgentBalance
{
...
public int AgentId { get; set; }
public virtual Agent Agent { get; set; }
}
AgentId is detected as the FK for the Agent relationship by convention, but I want to make it explicit in the Mapping class, to be safer against future changes. If Agent had a collection of Balances then I know how to do this e.g.:
HasRequired(t => t.Agent).WithMany(a => a.Balances).HasForeignKey(t => t.AgentId);
However, Agent does not have a collection of Balances - I don't want that association to be reverse navigable. But without the .WithMany in the mapping I don't get the option to specify .HasForeignKey. Is there another way? (N.B. I know I could also do this using attributes, but I want to use the fluent API mapping).
I believe you should be able to do this:
HasRequired(t => t.Agent).WithMany().HasForeignKey(t => t.AgentId)
这篇关于EF Code First Fluent API,指定Foreign Key属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!