Entity framework - Define connection where neither

2019-07-29 08:27发布

I have a problem, I have to create a model where we have two entities which CAN be linked together but can also exist as stand alone entities.

The model currently looks like this:

 public class Authorisation
 {
    public int AuthorisationID { get; set; }

    public virtual Change Change { get; set; }
}


public class Change
{
    public int ChangeID{ get; set; }

    public virtual Authorisation Authorisation { get; set; }

    public int? AuthorisationID{ get; set; }
}

The reason for this is that we can have an authorization record independent of a change, and some changes require authorisation and some dont, so neither side of the relationship is required.

I can configure this with the fluent API like so:

modelBuilder.Entity<Authorisation>()
        .HasOptional(t => t.Change)
        .WithOptionalPrincipal(t => t.Authorisation);

And alls well, Except that the migration that it creates looks like this

CreateTable(
            "dbo.Changes",
            c => new
                {
                    ChangeID = c.Int(nullable: false, identity: true),
                    AuthorisationID = c.Int(),
                    Authorisation_AuthorisationID = c.Int(),
                })
            .PrimaryKey(t => t.ChangeID)
            .ForeignKey("dbo.Authorisations", t => t.Authorisation_AuthorisationID)
            .Index(t => t.Authorisation_AuthorisationID);

EF is deciding that its going to add a new column (Authorisation_AuthorisationID) for me to use as the FK between the two entities, what I really want to be able to do is to use the change.AuthorisationID property as the FK onto the Authorisation, I cannot find a way to configure this at all (Please note that I need the FK to be in the model - for consistency with the rest of the app more than anything else).

To sum up I need to be able to create a relationship between two entities where both sides of the relationship are optional and if possible I want to be able to define the FK column myself.

Am I just approaching this wrong? Ive been staring at the same block of code for so long I could be missing something simple.

1条回答
三岁会撩人
2楼-- · 2019-07-29 08:39

Looks like explicit foreign key property is not supported for one-to-one relationships - there is no HasForeignKey Fluent API and also if you put ForeignKey attribute on the navigation property you get exception saying that multiplicity must be *.

So the only choice you have is to remove the explicit Change.AuthorisationID property and work only with navigation properties:

Model:

public class Authorisation
{
    public int AuthorisationID { get; set; }
    public virtual Change Change { get; set; }
}

public class Change
{
    public int ChangeID{ get; set; }
    public virtual Authorisation Authorisation { get; set; }
}

Configuration:

modelBuilder.Entity<Authorisation>()
    .HasOptional(t => t.Change)
    .WithOptionalPrincipal(t => t.Authorisation)
    .Map(a => a.MapKey("AuthorisationID"));
查看更多
登录 后发表回答