簡體   English   中英

EF Core:同時使用 ID 作為主鍵和外鍵

[英]EF Core: Using ID as Primary key and foreign key at same time

我有兩個實體,Prospect 和 person,我想要做的是使用 Prospect.ID 作為 ProspectTable 上的主鍵和 PersonID 的外鍵,我的想法是對兩個實體使用相同的 ID,而不需要我的 PersonID潛在實體。 當潛在客戶保存在數據庫中時,即使我的潛在客戶實體上沒有此屬性,它也會嘗試保存 PersonID,我想知道 ef core 是否支持這種關系。

這是我在我的模型構建器上得到的。

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) ,

人DTO:

public class PersonDto : DtoBase
{
    public PersonDto()
    {

    }

    public ProspectDto Prospect { get; set; }
}

前景DTO:

public class ProspectDto : DtoBase
{
    public ProspectDto()
    {

    }

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

數據基礎:

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

謝謝。

僅使用屬性,不使用 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);
        });
}

看起來非常接近你需要的東西。

這是@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.Entity<ProspectDto>().HasOne(p => p.Person).WithOne().HasForeignKey<ProspectDto>(p => p.ID);
}

如果您將關系建模為一對一,EF 將自動使用委托人的 PK 作為從屬人的 FK。

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

請注意, ProspectDto在 DB 上仍然會有一個ID列(從 DtoBase 繼承),但 FK 關系將在ProspectDto.IDPersonDto.ID之間,並且應該沒有ProspectDto.PersonId列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM