我正在使用Code First方法来处理当前的应用程序。我在创建多个相关的entites时遇到错误。
[Table("Project.WorkItems")]
public class WorkItem
{
public WorkItem()
{
Links = new List<WorkItemLink>();
States = new List<WorkItemStatus>();
}
[Key]
public int WorkItemId { get; set; }
[FieldRequired, StringLength(100)]
public string Title { get; set; }
[FieldRequired(FieldRequiredAttribute.FieldTypes.Select), StringLength(100)]
public string Iteration { get; set; }
public int Priority { get; set; }
[FieldRequired(FieldRequiredAttribute.FieldTypes.Select), StringLength(128), Display(Name = "Assigned To")]
public string AssignedTo { get; set; }
[FieldRequired(FieldRequiredAttribute.FieldTypes.Select)]
public int ActivityType { get; set; }
public string Description { get; set; }
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
public DateTime AddedOnDate { get; set; }
public int ProjectId { get; set; }
public int Status { get; set; }
public virtual ICollection<WorkItemLink> Links { get; set; }
public virtual ICollection<WorkItemStatus> States { get; set; }
}
[Table("Projects.WorkItemStatus")]
public class WorkItemStatus
{
[Key]
public int StatusId { get; set; }
public int WorkItemId { get; set; }
public virtual WorkItem WorkItem { get; set; }
public int ItemStatus { get; set; }
public bool IsCurrentStatus { get; set; }
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
public DateTime AddedOn { get; set; }
}
使用添加的功能
public async Task<EntityCreationResult<Models.WorkItem>> CreateWorkItemAsync(Models.WorkItem workitem)
{
try
{
//workitem.States = new List<Models.WorkItemStatus>();
if (workitem.Status == (int)Common.Helpers.Enumerations.WorkItemStatus.InProgress)
{
workitem.States.Add(new Models.WorkItemStatus { ItemStatus = (int)Common.Helpers.Enumerations.WorkItemStatus.ToDo, IsCurrentStatus = false });
workitem.States.Add(new Models.WorkItemStatus { ItemStatus = (int)Common.Helpers.Enumerations.WorkItemStatus.InProgress, IsCurrentStatus = true });
}
else
{
workitem.States.Add(new Models.WorkItemStatus { ItemStatus = (int)Common.Helpers.Enumerations.WorkItemStatus.ToDo, IsCurrentStatus = true });
}
var result = await Context.WorkItemRepository.AddAsync(workitem);
await Context.CommitChangesAsync();
if (result.Succeeded)
return result;
throw new Exception("Failed To Create Work Item");
}
catch (Exception ex)
{
throw;
}
}
public async Task<EntityCreationResult<T>> AddAsync<T>(T entity) where T : class
{
return await Task.Factory.StartNew<EntityCreationResult<T>>(() =>
{
if (entity == null) throw new ArgumentNullException("entity");
if (Context.Entry(entity).State == EntityState.Detached)
{
Context.Set<T>().Attach(entity);
}
Context.Set<T>().Add(entity);
return new EntityCreationResult<T>(entity);
});
}
模型创建
private void BuildRelationShips(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<WorkItem>().HasMany<WorkItemLink>(s => s.Links)
.WithRequired(s => s.WorkItem).HasForeignKey(s => s.WorkItemId);
modelBuilder.Entity<WorkItem>().HasMany<WorkItemStatus>(s => s.States)
.WithRequired(s => s.WorkItem).HasForeignKey(s => s.WorkItemId);
}
数据库的更改已成功提交,但出现错误 更新对象上下文时发生。 ObjectContext可能 处于不一致的状态。内部异常消息:参考 发生完整性约束违规:属性值 定义参照约束之间不一致 关系中的主体和依赖对象。