如何在 EF 6.4.4 中添加单向一对多关系?
How to Add an Unidirectional One to Many Relationships in EF 6.4.4?
我有 2 个 classes:
public class Client
{
public int Id { get; set; }
}
public class ClientRequest
{
public int Id { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
}
我无法以任何方式修改 Client
class(它是外部的);我想在这两个模型之间配置单向一对多关系。所以 Client 1 ← * ClientRequest
而我只能从 ClientRequest
导航到 Client
。我该怎么做?
在 EFCore 中我会这样做:
entity.HasOne(e => e.Client)
.WithMany()
.HasForeignKey(e => e.ClientId)
.OnDelete(DeleteBehavior.ClientSetNull);
但我在 EF 6.4.4 中没有这些方法。
会不会像这样简单:
modelBuilder?.Entity<ClientRequest>()
.HasRequired(e => e.Client);
在 EF 6.4.4 中配置如下关系:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ClientRequest>()
.HasRequired(x => x.Client)
.WithMany()
.HasForeignKey(x => x.ClientId)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
我有 2 个 classes:
public class Client
{
public int Id { get; set; }
}
public class ClientRequest
{
public int Id { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
}
我无法以任何方式修改 Client
class(它是外部的);我想在这两个模型之间配置单向一对多关系。所以 Client 1 ← * ClientRequest
而我只能从 ClientRequest
导航到 Client
。我该怎么做?
在 EFCore 中我会这样做:
entity.HasOne(e => e.Client)
.WithMany()
.HasForeignKey(e => e.ClientId)
.OnDelete(DeleteBehavior.ClientSetNull);
但我在 EF 6.4.4 中没有这些方法。
会不会像这样简单:
modelBuilder?.Entity<ClientRequest>()
.HasRequired(e => e.Client);
在 EF 6.4.4 中配置如下关系:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ClientRequest>()
.HasRequired(x => x.Client)
.WithMany()
.HasForeignKey(x => x.ClientId)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}