EF Core:同时使用ID作为主键和外键 [英] EF Core: Using ID as Primary key and foreign key at same time

查看:578
本文介绍了EF Core:同时使用ID作为主键和外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two entites, Prospect and person, what I'm trying to do is use Prospect.ID as primary key on ProspectTable and as foreignkey of PersonID, my ideia is use the same ID for both entities without the need of a PersonID on my Prospect entity. When the prospect is being saved on database, it tries to save a PersonID even I not have this property on my Prospect entity, I would like to know if ef core supports this kind of relationship.

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

Here's what I got on my model builder.

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); });

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

Here's what is being executed on database:

插入[潜在客户]([ID],[PersonID])值(@ p421,@ p422)

PersonDTO:

PersonDTO:

public class PersonDto : DtoBase
{
    public PersonDto()
    {

    }

    public ProspectDto Prospect { get; set; }
}

ProspectDTO:

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; }
}

谢谢。

推荐答案

仅使用属性,不使用FluentAPI:

Using attributes only, without 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();
}

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

I don't know what is equivalent of ForeignKey in FluentAPI. All other (Key and InverseProperty) are configurable, but why use two methods instead one.

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

Code above generates following migration code:

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);
        });
}

看起来非常接近您的需求。

Looks very close to what you need.

这篇关于EF Core:同时使用ID作为主键和外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆