与hasforeignkey一对一的关系

时间:2017-05-15 14:34:14

标签: c# entity-framework ef-fluent-api

我有以下课程。

class Appointment extends React.Component {
  getWidth(appt){
    return Math.abs((new Date(appt.end_at)-(new Date(appt.start_at))))/3600000
 }
  render () {
    var style = {
        width:this.getWidth(this.props.appointment) *'6.25%',
        height:'30px',
        background:'blue',
    };
    return (
        <div>
            <div id="rectangle" style={style}></div>
        </div>
    );
}
}

仅在class Zero { One One { get; set; } int OneId { get; set; } } class One { Zero Zero { get; set} } modelBuilder.Entity<Zero>() .HasRequired(e => e.One) .WithOptional(a=>a.Zero) .HasForeignKey(e => e.OneId);//<--error

之后HasForeignKey之后没有WithOptional

我可以做些什么来获得相同的结果?

1 个答案:

答案 0 :(得分:1)

在EF 6.X中,您可以对此stackoverflow帖子采取行动 Implementing Zero Or One to Zero Or One relationship in EF Code first by Fluent API

在EF Core中你可以尝试这个


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity()
                .HasOne(p => p.Link)
                .WithOne(i => i.UrlProvider)
                .HasForeignKey(b => b.UrlProviderId);
        }