user1765862
user1765862

Reputation: 14195

How to map one-to-many relationship using C# code mapping?

Account.cs

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

Alert.cs

public Account Account { get; set; }

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

and on the alert side

AlertMap.cs

ManyToOne(x => x.Account);

Can someone confirm that this mapping is correct?

Upvotes: 1

Views: 1479

Answers (1)

Firo
Firo

Reputation: 30813

  • when you have a backreference as in your example the onetomany mapping should have Inverse() set
  • it is better to specify the keycolumn on both sides explicitly to avoid creating 2 different foreign keys
  • consider setting cascading to something other than none to enable cascading save/update/delete
  • if Alerts can't stand on their own (without an Account) add 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"));

Note:

  • the generic argument can be infered by the compiler
  • this is of the top of my head, the syntax may differ

Upvotes: 1

Related Questions