本文介绍了获取“实体类型为"X"的实例的实例"异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想问为什么在2个表中插入多个数据时为什么会出现此错误.感谢您的高级回答.

I just want to ask why i am getting this error when I am trying to insert multiple data in 2 tables. Thank you for your answer in advanced.

这是我在异常处理程序中遇到的错误

这是我的源代码

public void CreateCollection(IEnumerable<CollectionViewModel> p , IEnumerable<Claim> user)
        {
            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();
        }

这是我的帐户日历事件实体

this is my account calendar events entity

public class Accountcalendarevents
    {
        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; }

    }

和我的收款实体

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; }

        [ForeignKey("AccountId")]
        public Accountcalendarevents Accountcalendarevents { get; set; }
}

推荐答案

您可以这样尝试:

_context.Accountcollection.AddRange(modules);
_context.SaveChanges();

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

这篇关于获取“实体类型为"X"的实例的实例"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 01:08