问题描述
我将EF Core与.Net Core 3.1一起使用
Im using EF Core with .Net Core 3.1
我有一个简单的Client-Event关系示例:
I have simple example of Client-Event relationship:
public class BaseEntity
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
}
public class Client : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
public class Event : BaseEntity
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public Client Client { get; set; }
}
在我的上下文中,我使用 FluentAPI 指定关系:
In my context, im using FluentAPI to specify relationships:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.HasOne<Client>()
.WithMany()
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
}
创建迁移时,Client Table看起来不错,但是事件表如下所示:
When I create migration, the Client Table looks fine, but Event table looks like this:
migrationBuilder.CreateTable(
name: "Events",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CreatedOn = table.Column<DateTime>(nullable: false),
ModifiedOn = table.Column<DateTime>(nullable: true),
Start = table.Column<DateTime>(nullable: false),
End = table.Column<DateTime>(nullable: false),
ClientId = table.Column<int>(nullable: false),
ClientId1 = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Events", x => x.Id);
table.ForeignKey(
name: "FK_Events_Clients_ClientId",
column: x => x.ClientId,
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Events_Clients_ClientId1",
column: x => x.ClientId1,
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
最后,我最终得到了两列: ClientId 和 ClientId1 。为什么EntifyFramework为我的关系创建两列?
Finally I end up having two columns: ClientId and ClientId1. Why is EntifyFramework creating two columns for my relationship?
到目前为止,我还没有使用FluentApi,它与影子属性ClientId自动生成的效果很好,但是我需要从该实体配置级联删除,并且由于没有另一种方式,我指定了上图所示的关系。从那时起,还有一个外键列。
I wasnt using FluentApi so far and it worked perfectly with just shadow property ClientId auto-generated, but i needed to configure cascade delete form this entities, and since there is no other way to do it, I specified the relationship as pictured above. Since then, there is additional foreign key column for it.
我尝试查看外键列:
modelBuilder.Entity<Event>()
.HasOne<Client>()
.WithMany()
.IsRequired()
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.Cascade);
到目前为止没有效果。有没有办法使用自动生成的阴影属性告诉EF im?
No effect so far. Is there any way to tell EF im using auto generated shadow properties?
编辑#1:
我也尝试像我自己那样自己查看外键属性:
I also tried specyfing Foreign keys properties on my own like this:
public class Event : BaseEntity
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public int ClientId { get; set; }
public Client Client { get; set; }
}
然后
modelBuilder.Entity<Event>()
.HasOne<Client>()
.WithMany()
.IsRequired()
.HasForeignKey(e => e.ClientId)
.OnDelete(DeleteBehavior.Cascade);
但仍然没有效果。
推荐答案
事实证明,关系可能是空的-决定权由框架决定,但这并不能真正满足您的兴趣。我修改了代码,因此显式指向了导航属性,EF识别出该关系并停止为列创建阴影属性:
It turns out, relationships can be empty - leaving decision to the framework, but it doesn't really serve Your interest. I modified my code, so there is explicit pointing at the navigation property, and EF recognized the relationship and stopped creating shadow properties for the columns:
modelBuilder.Entity<Event>()
.HasOne<Client>(e => e.Client)
.WithMany()
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
这篇关于EF Core创建多个外键列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!