1

i have 3 class

public class StockDef
{
    public int Id { get; set; }
    public int StockType { get; set; }
    public virtual OfficeStock SayanStock { get; set; }
    public virtual AgentStock AgentStock { get; set; }
    //....
}

public class OfficeStock
{
    public virtual StockDef StockDef { get; set; }
    //....
}

public class AgentStock
{
    public virtual StockDef StockDef { get; set; }
    //......
}

every StockDef object definitely have (one OfficeStock or one AgentStock).
and every OfficeStock definitely have one StockDef.
and every AgentStock definitely have one StockDef.
how can I implement this, in EF Code first by fluent API?
tnx

memo
  • 15
  • 1
  • 6
  • You need CHECK Constraint if you want : one `officeStock` or one `AgentStock` for `StockDef` and I think, it's not possible with fluent Api. but you can add it manually. Check this [link](http://stackoverflow.com/questions/34245449/is-it-possible-to-add-check-constraint-with-fluent-api-in-ef7) – iamnapo Oct 01 '16 at 20:03
  • tnx for response @iamnapo
    I should be how the final table in database?
    – memo Oct 02 '16 at 04:57

1 Answers1

0

You can't configure StockDef (the constraint "only one property can be compiled") with fluent api, you have to add a custom validation implementing IValidatableObject interface.

You can also add a constraint (a trigger?) on the database so you can avoid that someone else inserts wrong records.

Also, about 1-1 relationship configuration you can have a look here Code First migration when entities have cross references with foreign keys

Community
  • 1
  • 1
bubi
  • 6,414
  • 3
  • 28
  • 45
  • why?? you say it's not possible StockDef can only one OfficeStock or AgentStock? – memo Oct 02 '16 at 08:57
  • Probably I did not understand. StockDef can have one OfficeStock and one AgentStock? I mean, can be `StockDef.OfficeStock` and `StockDef.AgentStock` both compiled? – bubi Oct 02 '16 at 09:37
  • StockDef can have one OfficeStock OR one AgentStock – memo Oct 02 '16 at 12:07
  • I don't think that you can configure this constraint with fluent api. I think you have to implement `IValidatableObject ` otherwise you can have one OfficeStock AND one AgentStock. – bubi Oct 03 '16 at 05:39