gpt4 book ai didi

c# - Entity Framework 5.0b2 代码优先 : One-To-Many and One-To-One for the same table, 带级联删除

转载 作者:行者123 更新时间:2023-11-30 12:32:54 28 4
gpt4 key购买 nike

经过几个小时的反复试验,我找到了这个 thread其中解释了如何建立具有相同两种类型的一对多关系和一对一关系。

但是,我无法让它与级联删除一起使用:

Thrown: "Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values." (System.Data.UpdateException) Exception Message = "Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.", Exception Type = "System.Data.UpdateException"

只有在我不取消 1:1 关系(见下面的代码)时才会发生这种情况,我认为这是有道理的,因为它会创建一个无效的引用。我只是想知道是否有更好的方法来表示这一点。

示例代码:

class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<Context>());

using (var ctx = new Context())
{
var user = new User();

ctx.Users.Add(user);
ctx.SaveChanges();

var source = new PaymentSource();
user.PaymentSources = new Collection<PaymentSource>();
user.PaymentSources.Add(source);
user.DefaultPaymentSource = source;
ctx.SaveChanges();

// if I don't do this, I get ordering exception
user.DefaultPaymentSource = null;
ctx.SaveChanges();

ctx.Users.Remove(user);
ctx.SaveChanges();

Assert.Equal(0, ctx.Users.Count());
Assert.Equal(0, ctx.PaymentSources.Count());
}
}
}

public class User
{
public int Id { get; set; }

public virtual ICollection<PaymentSource> PaymentSources { get; set; }
public virtual PaymentSource DefaultPaymentSource { get; set; }
public int? DefaultPaymentSourceId { get; set; }
}

public class PaymentSource
{
public int Id { get; set; }
public virtual User User { get; set; }
public int UserId { get; set; }
}

public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<PaymentSource> PaymentSources { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<User>()
.HasOptional(u => u.DefaultPaymentSource)
.WithMany()
.HasForeignKey(u => u.DefaultPaymentSourceId)
.WillCascadeOnDelete(false);

modelBuilder.Entity<PaymentSource>()
.HasRequired(p => p.User)
.WithMany(p => p.PaymentSources)
.HasForeignKey(p => p.UserId)
.WillCascadeOnDelete();
}
}

最佳答案

我列出了其他选项来描述您的抽象:

A.

如何使用 3 个这样的表:

user 1-* paymentSource
user 1-0..1 DefaultPaymentSource
DefaultPaymentSource 0..1-1 PaymentSource

或者这个:

B.

user 1-* paymentSource
user 1-0..1 DefaultPaymentSource
DefaultPaymentSource --derive from--> PaymentSource

或者这个:

C.

user 1-* paymentSource
PaymentSource has addtional boolean field for "IsDefault"

我投票选 B 为最佳。

我敢肯定,从同一个源表到同一个目标表的两个关系不是一个好主意。它可能会破坏有关数据库最佳实践的一些规则或模式。

关于c# - Entity Framework 5.0b2 代码优先 : One-To-Many and One-To-One for the same table, 带级联删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10584341/

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