2017-01-30 45 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

對於任何一個實體框架的問題,谷歌/功能都會有這樣的網站在第一頁上面的結果 - 以一段時間,在提問之前做一些調查研究 - 每個人都非常樂意幫助解答,但通過研究和閱讀材料尋找解決問題的方法是真正價值的來源,因爲您將學習不僅僅是如何解決你當前的問題。

相關問題