EF 7(核心):另一个数据库中的依赖对象

时间:2016-04-14 05:11:05

标签: c# .net entity-framework-core

我是EF 7的新手,使用的是1.0.0-rc1-final 使用数据库第一次设计(数据库已经存在,所以必须使用它)

我有2个实体 - 父实体在一个数据库中,从属子实体在同一服务器实例上的另一个数据库中

我有两个数据库上下文,每个数据库一个,并相应地注册实体,但是当Eager加载父实体(使用include)时,它仍然试图在同一个数据库中找到依赖实体

我做错了什么?

在以下代码中,删除了与问题无关的所有其他对象

父:

[Table("tblEventsDates")]
public class Session
{
    [Column("EventDateID")]
    public int SessionId { get; set; }
    [Column("EventDateName")]
    public string Name { get; set; }               
    [Column("EventDate")]
    public DateTime SessionDate { get; set; }        
    [Column("EventDateTime")]
    public DateTime SessionTime { get; set; }
    [Column("EventDateMinutes")]
    public decimal? SessionDurationInMinutes { get; set; }
    [Column("EventDateArrival")]
    public DateTime? ArrivalTime { get; set; }
    [Column("EventCapacity")]
    public int SessionCapacity { get; set; }

    //navigation properties        
    [JsonIgnore]        
    public int EventId { get; set; }
    [JsonIgnore]        
    public Event Event { get; set; }
    public HardTicketRange HardTicketRange {get;set;}

}

依赖实体:

    [Table("tblhardTicketRanges")]
    public class HardTicketRange {
        public int HardTicketRangeId { get; set; }
        public int Start { get; set; }
        [Column("Finish")]
        public int End { get; set; }
        public string Description { get; set; }

        //navigation properties`enter code here`
        public int SessionId { get; set; }
        public Session Session { get; set; }

    }

第二个DbContext:

public class TicketingDataDbContext : DbContext {
    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.Entity<HardTicketRange>()
            .HasOne(e => e.Session)
            .WithOne(e => e.HardTicketRange);            

        modelBuilder.Entity<HardTicketRange>(entity => {
            entity.HasKey(e => e.HardTicketRangeId);
        });          

    }

    public virtual DbSet<HardTicketRange> HardTicketRanges { get; set; }
}

第一个DBContext

 public partial class TicketingCentralDbContext : DbContext {

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.Entity<Session>()
            .HasOne(e => e.HardTicketRange)
            .WithOne(b => b.Session);            

        modelBuilder.Entity<Session>(entity => {
            entity.HasKey(e => e.SessionId);                              
        });
    }

    public virtual DbSet<Session> Sessions { get; set; }
}

选择父实体的查询

var sessions = from s in _cxt.Sessions.Include(e=>e.HardTicketRange)
                       where s.EventId == eventId                                                      
                       orderby s.SessionDate, s.SessionTime  descending
                       select s;

EF 7生成的SQL - 由于2个表存在于不同的数据库中而不正确

SELECT [e].[EventDateID], [e].[EventDateArrival], [e].[EventId], [e].

[EventDateName], [e].[EventCapacity], [e].[EventDate], [e].[EventDateMinutes], [e].[EventDateTime], [t].[HardTicketRangeId], [t].[Description], [t].[Finish], [t].[SessionId], [t].[Start]
FROM [tblEventsDates] AS [e]
LEFT JOIN [tblhardTicketRanges] AS [t] ON [t].[SessionId] = [e].[EventDateID]
WHERE [e].[EventId] = @__eventId_0
ORDER BY [e].[EventDate], [e].[EventDateTime] DESC

0 个答案:

没有答案