EF Core:同时使用ID作为主键和外键

时间:2016-12-21 11:27:26

标签: c# entity-framework-core ef-model-builder

我有两个参与者,Prospect和person,我想要做的是使用Prospect.ID作为ProspectTable的主键和PersonID的外键,我的ideia对两个实体使用相同的ID而不需要我的Prospect实体上的PersonID。当潜在客户被保存在数据库中时,它会尝试保存PersonID,即使我的Prospect实体上没有此属性,我想知道ef核心是否支持这种关系。

这是我在模型构建器上获得的内容。

modelBuilder.Entity<ProspectDto>(builder => { builder.ToTable("Prospects"); builder.HasKey(prospect => prospect.ID); });

modelBuilder.Entity<PersonDto>(builder => { builder.HasOne(p => p.Prospect).WithOne().HasForeignKey<ProspectDto>(pe => pe.ID); });

这是在数据库上执行的内容:

INSERT INTO [Prospects] ([ID], [PersonID]) VALUES (@p421, @p422)

PersonDTO:

public class PersonDto : DtoBase
{
    public PersonDto()
    {

    }

    public ProspectDto Prospect { get; set; }
}

ProspectDTO:

public class ProspectDto : DtoBase
{
    public ProspectDto()
    {

    }

    public PersonDto Person { get; set; } = new PersonDto();
}

DtoBase:

public abstract class DtoBase
{
    public Guid ID { get; protected set; }
}

感谢。

3 个答案:

答案 0 :(得分:6)

仅使用属性,不使用FluentAPI:

public abstract class DtoBase
{
    [Key]
    public Guid ID { get; protected set; }
}

public class PersonDto : DtoBase
{
    [InverseProperty("Person")]
    public ProspectDto Prospect { get; set; }
}

public class ProspectDto : DtoBase
{
    [ForeignKey("ID")]           // "magic" is here
    public PersonDto Person { get; set; } = new PersonDto();
}

我不知道FluentAPI中ForeignKey的等价物。所有其他(Key和InverseProperty)都是可配置的,但为什么要使用两种方法而不是一种。

上面的代码生成以下迁移代码:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "Persons",
        columns: table => new
        {
            ID = table.Column<Guid>(nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Persons", x => x.ID);
        });

    migrationBuilder.CreateTable(
        name: "Prospects",
        columns: table => new
        {
            ID = table.Column<Guid>(nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Prospects", x => x.ID);
            table.ForeignKey(
                name: "FK_Prospects_Persons_ID",
                column: x => x.ID,
                principalTable: "Persons",
                principalColumn: "ID",
                onDelete: ReferentialAction.Cascade);
        });
}

看起来非常接近你需要的东西。

答案 1 :(得分:0)

以下是@dmitry解决方案的FluentAPI:

// Model classes:
public abstract class DtoBase
{
    public Guid ID { get; protected set; }
}

public class PersonDto : DtoBase
{
    public ProspectDto Prospect { get; set; }
}

public class ProspectDto : DtoBase
{
    public PersonDto Person { get; set; } = new PersonDto();
}

-------------------------------------------------------------------

// DbContext's OnModelCreating override:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasOne(p => p.Person).WithOne().HasForeignKey<ProspectDto>(p => p.ID);
}

答案 2 :(得分:-2)

如果您将关系建模为一:1,EF将自动使用主体的PK作为依赖的FK。

 ModelBuilder.Entity<ProspectDto>().HasRequired(p => p.Person).WithRequiredDependent();

请注意,ProspectDto在数据库上仍会有ID列(继承自DtoBase),但FK关系将介于ProspectDto.IDPersonDto.ID之间应该没有ProspectDto.PersonId列。