2013-02-18 153 views
1

Account.cs如何使用C#代码映射映射一对多关系?

public IList<Alert> Alerts { get; set; } 

Alert.cs

public Account Account { get; set; } 

Bag<Alert>(x => x.Alerts, c => { }, r => { r.OneToMany(); }); 

并对警报侧

AlertMap.cs

ManyToOne(x => x.Account); 

有人可以证实,这个映射是正确的?

+0

您使用的可能会有所不同'Fluent'。如果不是,您使用哪个库进行映射? – rae1 2013-02-19 04:47:42

+1

不使用流利。按代码映射。 – user1765862 2013-02-19 06:46:31

回答

1
  • 当你有一个反向引用,如您的示例一对多映射应该有Inverse()设置
  • 最好是指定两侧keycolumn明确,以避免产生2个不同的外键
  • 考虑设置级联到总比没有其他的东西,使级联保存/更新/删除
  • 如果警报不能自行站立(没有一个帐户)添加Cascade.DeleteOrphan

Bag(x => x.Alerts, c => { c.Inverse(); c.Key("account_id"); }, r => { r.OneToMany();});

ManyToOne(x => x.Account, c => c.Column("account_id"));

注:

  • 通用参数可以通过编译器
  • 这是我的头顶来infered,语法