简体   繁体   中英

EF6 Configuring a Required-to-Optional Relationship (One-to–Zero-or-One

I have two database tables called

SegmentSet SegmentSetGeometry

They both have primary key called SegmentSetId . (The SegmentSetGeometry (dependent) segmentSetId is foreign key to SegmentSet (primary)).

SegmentSets can have 0 or 1 SegmentSetGeometries

I have two classes representing these tables called SegmentSet and SegmentSetGeometry :

public class SegmentSet
{
    public long SegmentSetId { get; set; }
    // ...
    public virtual SegmentSetGeometry SegmentSetGeometry { get; set; }
}

public class SegmentSetGeometry
{
    public long SegmentSetId { get; set; }
    public DbGeometry Geometry { get; set; }

    public virtual SegmentSet SegmentSet { get; set; }
}

Here are their configurations:

public class SegmentSetConfiguration: EntityTypeConfiguration<SegmentSet>
{
    public SegmentSetConfiguration()
    {
        ToTable("SegmentSet");
        HasKey(x => x.SegmentSetId);

        // ...

        HasOptional(x => x.SegmentSetGeometry)
            .WithRequired(x => x.SegmentSet);
    }
}

public class SegmentSetGeometryConfiguration : EntityTypeConfiguration<SegmentSetGeometry>
{
    public SegmentSetGeometryConfiguration()
    {
        ToTable("SegmentSetGeometry");
        HasKey(x => x.SegmentSetId);
    }
}

When attempting to get a SegmentSet from the database the following error shows up:

Invalid column name 'SegmentSet_SegmentSetId'.

I found this reference https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships and decided to switch the relationship and tried:

public class SegmentSetConfiguration: EntityTypeConfiguration<SegmentSet>
{
    public SegmentSetConfiguration()
    {
        ToTable("SegmentSet");
        HasKey(x => x.SegmentSetId);

        // ...
    }
}

public class SegmentSetGeometryConfiguration : EntityTypeConfiguration<SegmentSetGeometry>
{
    public SegmentSetGeometryConfiguration()
    {
        ToTable("SegmentSetGeometry");
        HasKey(x => x.SegmentSetId);
        HasRequired(x => x.SegmentSet)
            .WithOptional(x=>x.SegmentSetGeometry);

    }
}

But still doesn't work. I am using the example they give and it's not working, and I have looked at all the similar stack overflows and still not working

To map these two together, you should map from the required side of the relationship, and only that one side. Mapping both sides can lead to EF getting a bit garbled which may be causing your issue.

public class SegmentSet
{
    public long SegmentSetId { get; set; }
    // ...
    public virtual SegmentSetGeometry Geometry { get; set; }
}

public class SegmentSetGeometry
{
    public long SegmentSetId { get; set; }
    // ...
    public virtual SegmentSet SegmentSet { get; set; }
}

public class SegmentSetConfiguration: EntityTypeConfiguration<SegmentSet>
{
    public SegmentSetConfiguration()
    {
        ToTable("SegmentSets");
        HasKey(x => x.SegmentSetId);
        // Note: Do not map the HasOptional here... Only map the required on the other side.
    }
}

public class SegmentSetGeometryConfiguration : EntityTypeConfiguration<SegmentSetGeometry>
{
    public SegmentSetGeometryConfiguration()
    {
        ToTable("SegmentSetGeometries");
        HasKey(x => x.SegmentSetId);
        HasRequired(x => x.SegmentSet)
            .WithOptional(x=>x.Geometry);

    }
}

That should work as expected using the PK's on each table for the 1..0/1 relationship.

Edit: It's also worth checking that the Entity configurations are being loaded. That might explain EF falling back on convention. In the DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Configurations.AddFromAssembly(GetType().Assembly);
}

The above assumes the Entity Type Configs are in the same assembly as the DbContext definition. Otherwise: typeof(SegmentSetGeometryConfiguration).Assembly would do the trick.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM