在2个表上插入多个数据时出现问题

时间:2019-03-20 08:30:37

标签: asp.net-mvc entity-framework

我在两个表中插入多个数据时遇到问题。 这是我的代码

上下文类

var userId = Convert.ToUInt32(user.Single(logon => logon.Type == CustomClaimTypes.UserId).Value);
            /*Create access table for insert*/

            var modules = p.Select(collection => new Accountcollection
            {
                AccountId = userId,
                Amount = collection.Amount,
                CashSource = collection.CashSource,
                CollectionDate = collection.CollectionDate,
                CreatedDatetime = DateTime.Now,
                UpdatedDatetime = DateTime.Now,
            }).ToList();

            _context.Accountcollection.AddRange(modules);

            var calendar_event = p.Select(collection => new Accountcalendarevents
            {
                AccountId = userId,
                Subject = collection.CashSource,
                Description = collection.CashSource,
                Start = collection.CollectionDate,
                End = collection.CollectionDate,
                ThemeColor = "blue",
                Isfullday = true,
                Status = "1",
                CreatedBy = userId,
                CreatedDatetime = DateTime.Now,
                UpdatedBy = userId,
                UpdatedDatetime = DateTime.Now
            }).ToList();

            _context.Accountcalendarevents.AddRange(calendar_event);
            _context.SaveChanges();
        }

这是我的收藏模型

public partial class Accountcollection
    {
        [Key]
        public long Id { get; set; }
        public long AccountId { get; set; }
        public double? Amount { get; set; }
        public string CashSource { get; set; }
        public DateTime CollectionDate { get; set; }
        public DateTime? CreatedDatetime { get; set; }
        public DateTime? UpdatedDatetime { get; set; }

        //public Accountcalendarevents Accountcalendarevents { get; set; }

        public virtual Accountmaster Account { get; set; }

    }

这是我的Caldendar Manager活动模型

public class Accountcalendarevents
    {
        [Key]
        public long Id { get; set; }   

        public long AccountId { get; set; }
        public string Subject { get; set; }
        public string Description { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public string ThemeColor { get; set; }
        public bool Isfullday { get; set; }
        public string Status { get; set; }
        public long CreatedBy { get; set; }
        public DateTime CreatedDatetime { get; set; }
        public long UpdatedBy { get; set; }
        public DateTime UpdatedDatetime { get; set; }

    }

问题是当我仅插入1个数据时工作正常,但是如果我尝试插入2个或更多,则出现这种异常 Exception i got

但是,当我发表评论

var calendar_event = p.Select(collection => new Accountcalendarevents
            {
                //AccountId = userId, <-- when I comment this line
                Subject = collection.CashSource,
                Description = collection.CashSource,
                Start = collection.CollectionDate,
                End = collection.CollectionDate,
                ThemeColor = "blue",
                Isfullday = true,
                Status = "1",
                CreatedBy = userId,
                CreatedDatetime = DateTime.Now,
                UpdatedBy = userId,
                UpdatedDatetime = DateTime.Now
            }).ToList();

它可以在多次插入数据时正常工作。

希望您对此有所帮助。谢谢!

0 个答案:

没有答案