EF6将插入参数的顺序错误

时间:2018-10-31 14:15:18

标签: entity-framework entity-framework-6

我正在首先将EF6 EDMX作品转换为EF6代码,主要是使用通过Visual Studio->添加项目-> Ado.net模型生成的现有数据库结构。

我有以下CF实体:

[Table("Account")]
public class Account
{
    [Key]
    public int AccountID { get; set; }

    public virtual ICollection<Message> Messages { get; set; }
    public virtual ICollection<MessageUser> MessageUsers { get; set; }

    internal static void ConfigureModel(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Account>()
            .HasMany(e => e.Messages)
            .WithRequired(e => e.Account)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Account>()
            .HasMany(e => e.MessageUsers)
            .WithRequired(e => e.Account)
            .WillCascadeOnDelete(false);
    }
}

[Table("Message")]
public class Message
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int MessageID { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int AccountID { get; set; }

    public virtual Account Account { get; set; }
    public virtual ICollection<MessageUser> MessageUsers { get; set; }

    internal static void ConfigureModel(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Message>()
            .HasMany(e => e.MessageUsers)
            .WithRequired(e => e.Message)
            .HasForeignKey(e => new { e.AccountID, e.MessageID })
            .WillCascadeOnDelete(false);
    }
}

[Table("MessageUser")]
public class MessageUser
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int MessageUserID { get; set; }

    [Key]
    [Column(Order = 1)]
    public int AccountID { get; set; }

    public int MessageID { get; set; }

    public virtual Account Account { get; set; }

    public virtual Message Message { get; set; }
}

在我的代码中,我通过以下方式将用户添加到邮件中:

        var messageUsers = new List<MessageUser>();
        foreach (var userId in usersInvolved)
        {
            var container = MessageContainer.INBOX;
            if (userId == _serviceContext.UserID)
            {
                container = messageModel.UserIDs.Contains(userId)
                    ? MessageContainer.INBOXANDSENT
                    : MessageContainer.SENT;
            }

            messageUsers.Add(new MessageUser
            {
                Message =  message,
                UserDetailID = userId,
                ParentMessageID = messageModel.ParentMessageID,
                LastUpdatedBy = _serviceContext.UserID,
                LastUpdatedOn = time,
                AccountID = _serviceContext.AccountID,
                Container = container
            });
        }

        DBContext.Message.Add(message);
        DBContext.MessageUser.AddRange(messageUsers);
        DBContext.SaveChanges();

这会导致消息用户和帐户之间的外键sql异常。当我加载sql profiler时,正在生成的插入语句是

exec sp_executesql N'INSERT [dbo].[MessageUser]([AccountID], [MessageID], [UserDetailID], [ProfileID], [ParentMessageID], [IsRead], [IsTrashed], [LastUpdatedBy], [LastUpdatedOn], [Container])
VALUES (@0, @1, @2, NULL, NULL, @3, @4, @5, @6, @7)
SELECT [MessageUserID], [RowVersionStamp]
FROM [dbo].[MessageUser]
WHERE @@ROWCOUNT > 0 AND [MessageUserID] = scope_identity() AND [AccountID] = @0',N'@0 int,@1 int,@2 int,@3 bit,@4 bit,@5 int,@6 datetime2(7),@7 varchar(20)',@0=17293,@1=10015,@2=10092,@3=0,@4=0,@5=11346,@6='2018-10-31 13:44:41.2743099',@7='Inbox'

与此相关的问题是17293是消息ID,10015是帐户ID。 EF6将这些参数的顺序错误,因此由于FK限制而失败。这非常令人担忧,因为我无法确定其他插入是否正常工作或意外工作,但存储的数据错误。

这是我的虫子吗?

0 个答案:

没有答案