具有两个 AppUser 引用的代码优先迁移建模 class
Code first migration modeling class with two AppUser references
我正在尝试使用代码优先迁移为用户的私人消息建模消息 class。
public class Message
{
public int MessageId { get; set; }
[Required]
public string MessageForUserId { get; set; }
public virtual ApplicationUser MessageForUser { get; set; }
[Required]
public string MessageFromUserId { get; set; }
public virtual ApplicationUser MessageFromUser { get; set; }
[Required]
public string MessageContent { get; set; }
public bool MessageRead { get; set; }
[Required]
public DateTime MessageCreatedOn { get; set; }
public DateTime? MessageReadOn { get; set; }
}
并且在 ApplicationUser class 我添加了两个集合
public class ApplicationUser : IdentityUser
{
// other properties removed for brevity
public ICollection<Message> UserMessages { get; set; }
public ICollection<Message> SentMessages { get; set; }
}
当我进行迁移时,我不仅获得了 MessageFrom 和 MessageFor Id,还向消息添加了两个额外的 ID ApplicationUser_Id 和 ApplicationUser _Id1 table。
我知道这是因为它们是消息中的两个外键。外键注释现在被注释掉了,因为我在迁移时遇到错误。
这是当外键处于活动状态并且我尝试迁移时出现的错误。
Unable to determine the relationship represented by navigation property 'ApplicationUser.UserMessages' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
我想使用流畅的注释在 OnModelCreating 方法中定义此 class 但我不确定如何正确地执行此操作。我显然不明白为什么 EF Code First 没有确定关系。
问题是我不确定如何定义消息 class 外键。每条消息都涉及两个用户:发送者和接收者。我考虑过将消息拆分为两个 classes,MessageSent 和 MessageReceived,但这无济于事,因为每个消息都有发送者和接收者。
任何有关如何使用流畅的注释正确定义它的帮助都将不胜感激。
更新:
根据 Dominick 的建议和之前的 post 参考资料,我尝试了以下无效的方法。
这是 DbSet 代码
public DbSet<Message> Messages { get; set; }
public DbSet<ApplicationUser> AppUsers { get; set; }
并在 OnModelCreating
builder.Entity(typeof(Message))
.HasOne(typeof(ApplicationUser), "MessageFromUser")
.WithMany()
.HasForeignKey("MessageFromUserId")
.OnDelete(DeleteBehavior.Restrict); // no ON DELETE
builder.Entity(typeof(Message))
.HasOne(typeof(ApplicationUser), "MessageForUser")
.WithMany()
.HasForeignKey("MessageForUserId")
.OnDelete(DeleteBehavior.Cascade); // set ON DELETE CASCADE
我收到与上述相同的错误,即使构建器选项有意义。该实体有一种类型的 ApplicationUser,MessagesFromUser,可以有很多并且外键 ID 是正确的。另一个 属性.
也是如此
builder.Entity<Message>()
.HasOne(x => x.MessageForUser)
.WithMany(x => x.UserMessages)
.HasForeignKey(x => x.MessageForUserId)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Message>()
.HasOne(x => x.MessageFromUser)
.WithMany(x => x.SentMessages)
.HasForeignKey(x => x.MessageFromUserId)
.OnDelete(DeleteBehavior.Cascade);
我正在尝试使用代码优先迁移为用户的私人消息建模消息 class。
public class Message
{
public int MessageId { get; set; }
[Required]
public string MessageForUserId { get; set; }
public virtual ApplicationUser MessageForUser { get; set; }
[Required]
public string MessageFromUserId { get; set; }
public virtual ApplicationUser MessageFromUser { get; set; }
[Required]
public string MessageContent { get; set; }
public bool MessageRead { get; set; }
[Required]
public DateTime MessageCreatedOn { get; set; }
public DateTime? MessageReadOn { get; set; }
}
并且在 ApplicationUser class 我添加了两个集合
public class ApplicationUser : IdentityUser
{
// other properties removed for brevity
public ICollection<Message> UserMessages { get; set; }
public ICollection<Message> SentMessages { get; set; }
}
当我进行迁移时,我不仅获得了 MessageFrom 和 MessageFor Id,还向消息添加了两个额外的 ID ApplicationUser_Id 和 ApplicationUser _Id1 table。
我知道这是因为它们是消息中的两个外键。外键注释现在被注释掉了,因为我在迁移时遇到错误。
这是当外键处于活动状态并且我尝试迁移时出现的错误。
Unable to determine the relationship represented by navigation property 'ApplicationUser.UserMessages' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
我想使用流畅的注释在 OnModelCreating 方法中定义此 class 但我不确定如何正确地执行此操作。我显然不明白为什么 EF Code First 没有确定关系。
问题是我不确定如何定义消息 class 外键。每条消息都涉及两个用户:发送者和接收者。我考虑过将消息拆分为两个 classes,MessageSent 和 MessageReceived,但这无济于事,因为每个消息都有发送者和接收者。
任何有关如何使用流畅的注释正确定义它的帮助都将不胜感激。
更新:
根据 Dominick 的建议和之前的 post 参考资料,我尝试了以下无效的方法。
这是 DbSet 代码
public DbSet<Message> Messages { get; set; }
public DbSet<ApplicationUser> AppUsers { get; set; }
并在 OnModelCreating
builder.Entity(typeof(Message))
.HasOne(typeof(ApplicationUser), "MessageFromUser")
.WithMany()
.HasForeignKey("MessageFromUserId")
.OnDelete(DeleteBehavior.Restrict); // no ON DELETE
builder.Entity(typeof(Message))
.HasOne(typeof(ApplicationUser), "MessageForUser")
.WithMany()
.HasForeignKey("MessageForUserId")
.OnDelete(DeleteBehavior.Cascade); // set ON DELETE CASCADE
我收到与上述相同的错误,即使构建器选项有意义。该实体有一种类型的 ApplicationUser,MessagesFromUser,可以有很多并且外键 ID 是正确的。另一个 属性.
也是如此builder.Entity<Message>()
.HasOne(x => x.MessageForUser)
.WithMany(x => x.UserMessages)
.HasForeignKey(x => x.MessageForUserId)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Message>()
.HasOne(x => x.MessageFromUser)
.WithMany(x => x.SentMessages)
.HasForeignKey(x => x.MessageFromUserId)
.OnDelete(DeleteBehavior.Cascade);