简体   繁体   English

获取'实体类型的实例'X''例外

[英]Getting 'The instance of entity type 'X'' Exception

I just want to ask why i am getting this error when I am trying to insert multiple data in 2 tables. 我只想问我为什么在尝试在2个表中插入多个数据时出现此错误。 Thank you for your answer in advanced. 谢谢你的高级答案。

This the the error I am getting in exception handler 这是我在异常处理程序中得到的错误

This is my Source Code 这是我的源代码

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

    }

and my account collection entity 和我的帐户收款实体

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

You may try like this: 你可以尝试这样:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “实体类型‘X’的实例映射到具有键值 Guid 的同一行,但具有不同的原始属性”异常是什么意思 - What does mean "The instance of entity type 'X' are mapped to the same row with the key value Guid, but have different original property" exception EF:无法跟踪实体类型X的实例,因为已经跟踪了具有相同密钥的此类型的另一个实例 - EF: The instance of entity type X cannot be tracked because another instance of this type with the same key is already being tracked 从实体框架获取错误(类型初始化器异常) - Getting an error from Entity Framework(type initializer exception) 实体框架:无法跟踪类型 x 的实体,因为已跟踪具有相同键的另一个实例 - Entity Framework: Entity of type x cannot be tracked because another instance with same key is already being tracked 错误:无法跟踪实体类型“X”的实例,因为已在跟踪另一个具有键值“{ID:3}”的实例 - Error: The instance of entity type 'X' cannot be tracked because another instance with the key value '{ID: 3}' is already being tracked “无法跟踪实体类型‘用户’的实例,因为已使用 UserManager 跟踪另一个具有键值‘{x}’的实例 - "The instance of entity type 'User' cannot be tracked because another instance with the key value '{x}' is already being tracked with UserManager 无法跟踪实体类型“x”的实例,因为已在跟踪另一个具有键值“{Id: 6}”的实例 - The instance of entity type 'x' cannot be tracked because another instance with the key value '{Id: 6}' is already being tracked EF Core(内存数据库)-'无法跟踪实体类型 X 的实例,因为另一个具有键值的实例 - EF Core (in-memory database) - 'The instance of entity type X cannot be tracked because another instance with the key value 无法跟踪实体类型 X 的实例,因为已跟踪具有键值“{Id:}”的另一个实例 - The instance of entity type X cannot be tracked because another instance with the key value '{Id:}' is already being tracked "使用 AsNoTracking() 获取“无法跟踪实体类型的实例,因为已经在跟踪具有相同键值的另一个实例”" - Getting 'The instance of entity type cannot be tracked because another instance with same key value for is already being tracked' with AsNoTracking()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM