sql server - Auto assign column value when row is created -
i using ef6 , sql server 2014.
i have following simplified model:
public class order { public int id { get; set; } public datetime created { get; set; } public ienumerable<orderitem> orderitems { get; set; } } public class orderitem { public int id { get; set; } public int orderid { get; set; } public order order { get; set; } public string itemname { get; set; } public int orderitemnumber { get; set; } }
the id properties standard auto-increment primary keys.
the orderitem.orderitemnumber property uniquely identifies orderitem within context of given order.
when new orderitem row inserted needs assigned next orderitemnumber parent order, select coalesce(max(orderitemnumber),0) dbo.orderitems orderid = 2
is there means using ef6/sql server auto-assign orderitemnumber @ point row inserted?
although proved interesting learning experience on ef support triggers (see https://github.com/nickstrupat/entityframework.triggers) decided re-model.
in case question result of incorrect modelling. orderitemnumber property redundant removed it; sequence orderitems created can inferred id value, , there no real requirement allow re-sequencing.
Comments
Post a Comment