I am trying to set up a relationship where a Car can have multiple Testruns, as well as an optionally selected "active testrun".
public class Car
{
public int ID { get; set; }
public virtual TestRun ActiveTestRun { get; set; }
public virtual ICollection<TestRun> TestRuns { get; set; }
}
public class TestRun
{
public int ID { get; set; }
public double TopSpeed { get; set; }
public virtual Car ActiveCar { get; set; }
public virtual Car Car { get; set; }
}
I have been trying to use InverseProperties with or without ForeignKey-attributes but to no luck. What is the correct way to setup this kind of relation? TIA!
Ps. I think I tried mostly all of the combinations from this tutorial without getting it to work :( http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-code-first.aspx
EDIT: The resulting DB-schema I would want is something like:
tbl_Cars: ID, ActiveTestRunID[Nullable]
tbl_TestRuns: ID, TopSpeed, CarID
This achieved the result that I wanted:
public class Car
{
public int ID { get; set; }
public int? ActiveTestRunID { get; set; }
public virtual TestRun ActiveTestRun { get; set; }
[InverseProperty("Car")]
public virtual ICollection<TestRun> TestRuns { get; set; }
}
public class TestRun
{
public int ID { get; set; }
public double TopSpeed { get; set; }
public int CarID { get; set; }
public virtual Car Car { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.