2017-01-30 107 views
0

我试图用实体框架代码优先(包括流利的API映射)方法创建一对一的映射。这是我第一次使用代码优先的方法。实体框架代码优先方法一对一流畅的api映射

当我运行UpdateTaskCompleted()方法,它会抛出以下异常:

操作数类型冲突:唯一标识符是与诠释

我怀疑我做错了流利的API不兼容映射。

[Table("tblSession")] 
public partial class tblSession 
{ 
     [Key] 
     public Guid SessionId { get; set; } 

     [Required] 
     public bool IsActive { get; set; } 

     public tblTaskDetail tblTaskDetail { get; set; } 
} 

[Table("tblTaskDetail")] 
public partial class tblTaskDetail 
{ 
    [Key] 
    public int TaskDetailID { get; set; } 

    public Guid? SessionID { get; set; } 

    [Required] 
    [StringLength(50)] 
    public string TaskStatus { get; set; } 

    [ForeignKey("SessionID")] 
    public tblSession tblSession { get; set; } 
} 

public class RequestSession 
{ 
    [Key] 
    public Guid SessionId { get; set; } 
    public bool IsActive { get; set; } 
    public TaskDetail TaskDetail { get; set; } 
} 

public class TaskDetail 
{ 
    [Key] 
    public int TaskDetailID { get; set; } 
    public Guid? SessionID { get; set; } 
    public string TaskStatus { get; set; } 
    public RequestSession RequestSession { get; set; } 
} 

public class TaskDetailMapper:EntityTypeConfiguration<TaskDetail> 
{ 
    public TaskDetailMapper() 
    { 
     this.ToTable("tblTaskDetail"); 
     this.HasKey(hk => hk.TaskDetailID); 
     HasRequired<RequestSession>(a => a.RequestSession) 
          .WithRequiredPrincipal(o => o.TaskDetail).Map(m => m.MapKey("SessionID")); 

     this.Property(o => o.TaskStatus).HasColumnName("TaskStatus"); 
    } 
} 

public class RequestSessionMapper : EntityTypeConfiguration<RequestSession> 
{ 
    public RequestSessionMapper() 
    { 
     // Table & Column Mappings 
     this.ToTable("tblSession"); 

     //Primary key 
     this.HasKey<Guid>(hk => hk.SessionId); 
     this.Property(t => t.SessionId).HasColumnName("SessionId"); 
     this.Property(t => t.IsActive).HasColumnName("IsActive"); 
    } 
} 

public partial class WarehouseAPIContext : DbContext 
{ 
    public WarehouseAPIContext(): base("name=WarehouseAPIContext") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    {   
     modelBuilder.Configurations.Add(new RequestSessionMapper()); 
     modelBuilder.Configurations.Add(new TaskDetailMapper()); 
    } 
} 

public TaskDetail UpdateTaskCompleted(TaskDetail entity) 
{ 
     try 
     { 
      var entry = dbSet.Find(entity.TaskDetailID); 
      entry.TaskStatus = entity.TaskStatus; 

      entity.RequestSession = new RequestSession() 
      { 
       IsActive = false 
      }; 

      _context.SaveChanges(); 

      return entity; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
} 

回答

0

TaskDetail.Id的类型为int,Session.Id的类型为Guid。

0

首先,我会选择使用Annotation或FluentAPI来配置您的模型。有些边缘案例的特征只能用一种方法完成,而不能用其他方法完成,但这些特征只有少数几个并且有据可查。

我使用FluentAPI,因为它更具表现力,并允许将所有配置集中在一个地方。

你需要做什么,就是看看这个非常好的资源上的EF的关系:http://www.entityframeworktutorial.net/entity-relationships.aspx

对于任何一个实体框架的问题,谷歌/功能都会有这样的网站在第一页上面的结果 - 以一段时间,在提问之前做一些调查研究 - 每个人都非常乐意帮助解答,但通过研究和阅读材料寻找解决问题的方法是真正价值的来源,因为您将学习不仅仅是如何解决你当前的问题。

相关问题