[英]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.