gpt4 book ai didi

entity-framework - 与 OneWay CascadeOnDelete 的一对一或零关系

转载 作者:行者123 更新时间:2023-12-04 07:23:32 25 4
gpt4 key购买 nike

我有一个实体,其中包含许多不同的文档,这些文档与我的数据库中的任何实体都有未知关系。

public class Document : BaseEntity
{

public string Filename { get; set; }

public string MIMEType { get; set; }
public int? Length { get; set; }

public byte[] Content { get; set; }

}

代码优先映射是:

    public DocumentConfiguration()
{
Property(x => x.Filename).HasMaxLength(300).IsRequired();
Property(x => x.MIMEType).HasMaxLength(300).IsRequired();
Property(x => x.Length).IsOptional();
Property(x => x.Content).IsOptional().HasColumnType("varbinary(max)");


ToTable("Document");
}

现在我想在我的地址中有一个与文档表的可选关系,如下所示:

public class Address : BaseEntity
{

public string Name1 { get; set; }
public string Name2 { get; set; }
public string Additional { get; set; }


public string Street { get; set; }
public string HousNr { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }

public virtual Document Image { get; set; }

}

具有以下映射:

    public AddressConfiguration()
{

Property(x => x.Name1).IsRequired().HasMaxLength(250);
Property(x => x.Name2).HasMaxLength(250);
Property(x => x.Additional).HasMaxLength(250);


Property(x => x.Street).HasMaxLength(250);
Property(x => x.HousNr).HasMaxLength(10);
Property(x => x.ZipCode).HasMaxLength(10);
Property(x => x.City).HasMaxLength(100);


HasOptional(x => x.Image)
.WithOptionalDependent()
.Map(map => map.MapKey("ImageId")).WillCascadeOnDelete();


ToTable("Address");

}

但是当我删除文档表中的图像时,它会删除相关地址。

我想要一个从地址到文件的单向删除,而不是从文件到地址...?

我该如何实现?

谢谢。

最佳答案

它具有从文档到地址的级联的原因是因为您使用了 WithOptionalDependent方法。来自文档:

Configures the relationship to be optional:optional without a navigation property on the other side of the relationship. The entity type being configured will be the dependent and contain a foreign key to the principal. The entity type that the relationship targets will be the principal in the relationship.

考虑 AddressConfiguration 方法中的这行代码:

HasOptional(x => x.Image)         // The entity type being configured is Address
.WithOptionalDependent()... // The entity type that the relationship targets
// is Document (x.Image)

这意味着您有效地将 Address 指定为该关联中的依赖项,将 Document 指定为主体,从而产生级联行为。

但是等等,这个故事还有更多!您可以通过两种方式创建一对一关联。首先是共享主键关联一对一外键关联。您可以从 here 阅读更多关于它们的信息和 here .看起来您想通过外键(一对一外键关联)映射您的关联。如果是这样,您必须注意依赖实体将始终携带外键,这意味着在您的情况下,Document 实体将有一个 AddressId 引用 AddressId 上的 Address 实体(您做了相反的操作,这是不正确的。)。

综上所述,您的对象模型和流畅的 API 代码应该如下所示:

public class Address 
{
public int AddressId { get; set; }
public virtual Document Image { get; set; }
}

public class Document
{
public int DocumentId { get; set; }
}

class Context : DbContext
{
public DbSet<Address> Addresses { get; set; }
public DbSet<Document> Documents { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Address>() // The entity being configured is Address
.HasOptional(x => x.Image)
.WithOptionalPrincipal()
.Map(map => map.MapKey("AddressId"))
.WillCascadeOnDelete();
}
}

基本上是 WithOptionalPrincipal是你应该使用的方法:

Configures the relationship to be optional:optional without a navigation property on the other side of the relationship. The entity type being configured will be the principal in the relationship. The entity type that the relationship targets will be the dependent and contain a foreign key to the principal.


因此,从地址到文档的级联删除已正确打开。

关于entity-framework - 与 OneWay CascadeOnDelete 的一对一或零关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10012267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com