【发布时间】:2011-07-15 09:01:25
【问题描述】:
我遇到了与你们中的一些人相同的问题 - 尝试插入新对象时,EF 为其某些属性插入空值,并且插入失败。
首先让我描述一下我们数据库的结构。它是一个事件管理系统,其中每个事件都需要与一个实践组相关联,存储在缓存表中,但最终从 Active Directory 中获取。我手动创建了连接表 - 有问题吗?反正Event有一个外键指向EventPracticeGroup,EventPracticeGroup有一个外键指向PracticeGroupCache。 PracticeGroupCache 还有一个 RegionId 指向 Regions 表。
在尝试插入新的 EventPracticeGroup 对象时出现问题。以下是我目前正在使用的代码:
var eventPracticeGroup = new EventPracticeGroup();
if (TryUpdateModel<EventPracticeGroup>(eventPracticeGroup))
{
/*
var eventId = EventScheduleRepository.GetById(Convert.ToInt32(Request.QueryString["EventScheduleId"])).EventId;
eventPracticeGroup.Event = EventRepository.GetById(eventId);
eventPracticeGroup.PracticeGroupCache = PracticeGroupCacheRepository.GetById(eventPracticeGroup.PracticeGroupCacheId);
eventPracticeGroup.PracticeGroupCache.Region = RegionRepository.GetById(eventPracticeGroup.PracticeGroupCache.RegionId);
EventPracticeGroupRepository.Add(eventPracticeGroup);
*/
var eventId = EventScheduleRepository.GetById(Convert.ToInt32(Request.QueryString["EventScheduleId"])).EventId;
var theEvent = new Event() { Id = eventId };
EventRepository.Repository.UnitOfWork.Context.AttachTo("Events",theEvent);
var practiceGroupCache = new PracticeGroupCache() { Id = eventPracticeGroup.PracticeGroupCacheId };
practiceGroupCache.Region = new Region() { Id = eventPracticeGroup.PracticeGroupCache.RegionId };
eventPracticeGroup.PracticeGroupCache = practiceGroupCache;
EventPracticeGroupRepository.Add(eventPracticeGroup);
EventPracticeGroupRepository.Save();
return RedirectToAction("Index");
}
无论如何...如您所见,我刚刚尝试使用存根对象(没有帮助),并且我也尝试过实际获取和设置对象。我得到的错误是:
无法将值 NULL 插入“名称”列、表“XXXX.dbo.Regions”;列不允许空值。插入失败。该语句已终止。
显然名称不是关键字段。我检查了 EDMX XML - 只有 Id(主键列)将 StoreGeneratedPattern 设置为 Identity,因为它们应该(它们是 int32 标识列)。没有一个外键将 StoreGeneratedPattern 设置为标识。
如果我将 Regions.Name 设置为允许空值,则 PracticeGroupCaches.Description 会引发相同的错误。似乎每个链接对象都设置为空。我确实看过调试器,当我使用现在注释掉的代码时,没有什么是空的,一切都有一个值。我什至让 RegionRepository 返回所有区域,只是为了查看其中一个区域是否有一个空名称。没有。我的测试数据库中只有 2 个。我们的对象上下文是每个 HTTP 请求共享的。
请任何人帮忙。在这一点上,只要可行,我就会满足于使用最肮脏的解决方法。
问候, 乔纳森。
【问题讨论】:
标签: entity-framework asp.net-mvc-2