Entity Framework: one to zero or one foreign key relationship

I have a 1: 0..1 ratio that I would like to map to EF 6 using the free API. A relationship consists of a principal, which may or may not be dependent. The dependent must always have a principal.

Basically, I need to have access to the identifier of the dependent.

My code is as follows:

public class Principal
{
    public int Id {get; private set; }

    public int? DependentId { get; private set; }
    public virtual Dependent Dependent { get; private set; }
}

public class Dependent
{
    public int Id { get; private set; }

    public virtual Principal Principal { get; private set; }
}

My mapping is as follows:

    public class PrincipalMap : EntityTypeConfiguration<Principal>
    {
        public PrincipalMap()
        {
            ToTable("PRINCIPALS");

            HasKey(x => x.Id);

            Property(x => x.Id)
                .HasColumnName("PRINCIPALID")
                .IsRequired()
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            Property(x => x.DependentId)
                .HasColumnName("DEPENDENTID")
                .IsOptional();
        }
    }

    public class DependentMap : EntityTypeConfiguration<Dependent>
    {
        public DependentMap()
        {
            ToTable("DEPENDENTS");

            HasKey(x => x.Id);

            Property(x => x.Id)
                .HasColumnName("DEPENDENTID")
                .IsRequired()
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            HasRequired(x => x.Principal).WithOptional(x => x.Dependent).Map(x => x.MapKey("PRINCIPALID")).WillCascadeOnDelete();
        }
    }

This results in the following migration:

            CreateTable(
                "dbo.PRINCIPALS",
                c => new
                    {
                        PRINCIPALID = c.Int(nullable: false, identity: true),
                        DEPENDENTID = c.Int(),
                    })
                .PrimaryKey(t => t.PRINCIPALID);

            CreateTable(
                "dbo.DEPENDENTS",
                c => new
                    {
                        DEPENDENTID = c.Int(nullable: false, identity: true),
                        PRINCIPALID = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.DEPENDENTID)
                .ForeignKey("dbo.PRINCIPALS", t => t.PRINCIPALID, cascadeDelete: true)
                .Index(t => t.PRINCIPALID);

As you can see, the column is DEPENDENTIDnot a foreign key. When the program starts and the dependent object is compared with the director, the property remains empty , that is, EF does not recognize it as related to the dependent itself. DEPENDENTID

What am I doing wrong?

+4
2

DependentMap DEPENDENTID DEPENDENT, (), . , ( ).

, EF ( E/R) ( ), 1-0..1. ( NULL).

:

public class Principal
{
    public int Id { get; private set; }

    public virtual Dependent Dependent { get; private set; }
}

public class Dependent
{
    public int Id { get; private set; }

    public virtual Principal Principal { get; private set; }
}

public class PrincipalMap : EntityTypeConfiguration<Principal>
{
    public PrincipalMap()
    {
        ToTable("PRINCIPALS");

        HasKey(x => x.Id);

        Property(x => x.Id)
            .HasColumnName("PRINCIPALID")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

public class DependentMap : EntityTypeConfiguration<Dependent>
{
    public DependentMap()
    {
        ToTable("DEPENDENTS");

        HasKey(x => x.Id);

        Property(x => x.Id)
            .HasColumnName("DEPENDENTID")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        HasRequired(x => x.Principal).WithOptional(x => x.Dependent).Map(x => x.MapKey("PRINCIPALID")).WillCascadeOnDelete();
    }
}

stataments ( EF)

ExecuteNonQuery==========
CREATE TABLE [DEPENDENTS] (
 [DEPENDENTID] int not null identity(1,1)
, [PRINCIPALID] int not null
);
ALTER TABLE [DEPENDENTS] ADD CONSTRAINT [PK_DEPENDENTS_204c4d57] PRIMARY KEY ([DEPENDENTID])
ExecuteNonQuery==========
CREATE TABLE [PRINCIPALS] (
 [PRINCIPALID] int not null identity(1,1)
);
ALTER TABLE [PRINCIPALS] ADD CONSTRAINT [PK_PRINCIPALS_204c4d57] PRIMARY KEY ([PRINCIPALID])
ExecuteNonQuery==========
CREATE INDEX [IX_PRINCIPALID] ON [DEPENDENTS] ([PRINCIPALID])
ExecuteNonQuery==========
ALTER TABLE [DEPENDENTS] ADD CONSTRAINT [FK_DEPENDENTS_PRINCIPALS_PRINCIPALID] FOREIGN KEY ([PRINCIPALID]) REFERENCES [PRINCIPALS] ([PRINCIPALID])

( , ).

E/R ( , EF).
BTW, Principal.Dependent, EF , selected * from dependent where PRINCIPALID = <principal_id>, id id , .

, , Dependent.Id Principal dependentId = Principal.Dependent.Id (, , dependentId = Principal.Dependent == null ? null : Principal.Dependent.Id).

, PRINCIPAL, DEPENDENT?
, EF ( ).
, R- , , DEPENDENT.PRINCIPALID PRINCIPAL, PRINCIPAL.DEPENDENTID DEPENDENT.
PRINCIPAL.DEPENDENTID(.. DEPENDENTID, EF ).

+1

, EF IMO. , , - 1: M:

HasRequired(x => x.Principal)
  .WithMany()
  .HasForeignKey(x => x.DependentId);
  .WillCascadeOnDelete();

http://weblogs.asp.net/manavi/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations

0

Source: https://habr.com/ru/post/1652628/


All Articles