c# - In Entity Framework 6 how can I create a one to many mapping with a navigation property on the other side? -
my domain model trade
can have many tradeleg
s. trying use following mappings.
trade mapping
haskey(t => t.tradeid); property(t => t.tradeid).hasdatabasegeneratedoption(databasegeneratedoption.identity) .hascolumnname("tradeid"); property(t => t.description).hascolumnname("description"); property(t => t.counterpartyid).hascolumnname("counterpartyid"); property(t => t.quantity).hascolumnname("quantity"); property(t => t.isactive).hascolumnname("isactive"); hasmany(t => t.tradelegs); totable("trade");
trade leg mapping
haskey(t => t.tradelegid); property(t => t.tradelegid).hasdatabasegeneratedoption(databasegeneratedoption.identity) .hascolumnname("tradelegid"); property(t => t.trdid).hascolumnname("trdid"); // fk trade property(t => t.ordinal).hascolumnname("ordinal"); property(t => t.description).hascolumnname("description"); totable("tradeleg");
when try select trades excpetion.
invalid column name
trade_tradeid
.
i can make work adding navigation property on tradeleg trade , adding .withrequired(u => u.trade).hasforeignkey(u =>u.trdid)
hasmany()
mapping on trade
object.
however, don't want have navigation property on tradeleg
trade
. want have int fk property (trdid
) on tradeleg
. noticed if use tradeid
instead of trdid
fk property on tradeleg
works because of default rules. there way tell ef name of fk field on other side of hasmany()
.
the mapping should work trade
object using hasmany()
. below should work.
hasmany(t => t.tradelegs) .withrequired() //use withoptional optional no need specify mapping on other side. .hasforeignkey(leg => leg.trdid);
i tested on ef6 on 1 of mapping objects , seems work.
this same concept can work other way when one-to-many on child without navigation property on parent such as
hasrequired(x => x.property) .withmany() //no need specify navigation property on other side .hasforeignkey(x=> x.foreignkeyproperty);
Comments
Post a Comment