Я столкнулся с исключением при вставке новой записи в базу данных с помощью Entity Framework Core. Я боролся с этим исключением почти 2 дня безуспешно.
Вот код:
if (orgUnits.OrganizationUnits != null)
{
foreach (OrgUnit unit in orgUnits.OrganizationUnits.ToList())
{
if (!groups.Any(x => x.OrgUnitPath == unit.OrgUnitPath))
{
foreach (User user in fetchedUsers)
{
if (unit.OrgUnitPath == user.OrgUnitPath)
{
if (curUsers.Exists(x => x.googleId == user.Id))
{
curUser = _domainUserService.GetByEmail(user.PrimaryEmail);
newUnitUsers.Add(curUser);
}
else
{
var photoUrl = googleServices.GetUserPicture(directoryService, user.PrimaryEmail, company.companyId);
curUser = _domainUserService.Convert(user, company, defaultSignature, photoUrl);
curUser.isLocked = true;
if (curUser != null)
{
_context.Add(curUser);
newUnitUsers.Add(curUser);
}
}
}
curUser = new DomainUser();
}
_context.SaveChanges();
var defaultCompanyProfile = company.companyProfiles.First();
var newDomainGroup = new DomainGroup()
{
Name = unit.Name,
assignedProfile = defaultCompanyProfile,
Company = company,
assignedSignature = defaultSignature,
Etag = unit.ETag,
OrgUnitId = unit.OrgUnitId,
OrgUnitPath = unit.OrgUnitPath,
ParentOrgUnitId = unit.ParentOrgUnitId,
ParentOrgUnitPath = unit.ParentOrgUnitPath,
Users = newUnitUsers,
isCustom = false
};
domainGroupsNEW.Add(newDomainGroup);
_context.Add(newDomainGroup); <---- Here is where the error is occurs
_context.SaveChanges();
newUnitUsers = new List<DomainUser>();
}
}
}
Entity Class:
public class DomainGroup
{
public int Id { get; set; }
public string Name { get; set; }
public virtual IEnumerable<DomainUser> Users { get; set; }
public virtual IEnumerable<Campaign> Campaigns { get; set; }
public virtual Signature assignedSignature { get; set; }
public virtual Company Company { get; set; }
public virtual CompanyProfile assignedProfile { get; set; }
public string OrgUnitPath { get; set; }
public string OrgUnitId { get; set; }
public string ParentOrgUnitId { get; set; }
public string ParentOrgUnitPath { get; set; }
public string Etag { get; set; }
public bool isCustom { get; set; }
public virtual DomainGroup parentGroup { get; set; }
}
Stack Trace:
System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.TryGetEntry(Object entity, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, ISet`1 handledForeignKeys, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node, Boolean force)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity)
at PanelServices.SyncService.OrgUnitsSync(String customerId, String adminEmail, Company company)
Я также пытался вставить записи с помощью AddRange (), но это не сработало.
Любая помощь будет принята с благодарностью.
Спасибо, с наилучшими пожеланиями .