Kann Code-first Entity Framework database-Abfragen mit SQL server-DBs auf demselben Feld ausführen?

Ich weiß, dass es eine ganze Reihe von Fragen über Entity Framework, die Cross-database-Abfragen auf dem gleichen server gepostet, um Stackoverflow . Meistens scheint die Antwort "nein" zu sein, und dieser Link aus dem path zurück im Jahr 2008 wird referenziert . Allerdings ändert Entity Framework die ganze time und mit CTP5 aus, ich frage mich, ob die Antwort immer noch die gleiche ist – das kannst du nicht machen, oder du kannst es tun, wenn du die edmx file manuell bearbeite oder du hast viewen verwenden. Diese function allein ist der Grund, warum ich immer an Linq-to-SQL gebunden bin, da wir mehrere SQL server 2008-databaseen auf demselben server haben und über sie abfragen müssen. Die Verschmutzung unserer databaseen mit Hunderten von select * views ist keine Option, und mit Code-First-Entwicklung habe ich keine Edmx-file zu bearbeiten. Ich spielte mit der Kneipendatenbank, um zu sehen, ob ich irgendwo hingehen könnte, aber ich stecke fest. Irgendwelche Vorschläge?

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } mit System; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } mit System.Linq; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } public string Vorname {get; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } public string LastName {get; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } } using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace DbSchema { public class Employee { [Key] public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public short JobID { get; set; } public Job Job { get; set; } } public class Job { [Key] public short ID { get; set; } public string Description { get; set; } } public class PubsRepository : DbContext { public DbSet<Employee> Employee { get; set; } public DbSet<Job> Job { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // employee var eeMap = modelBuilder.Entity<Employee>(); eeMap.ToTable("employee", "dbo"); // <-- how do I reference another database? eeMap.Property(e => e.ID).HasColumnName("emp_id"); eeMap.Property(e => e.FirstName).HasColumnName("fname"); eeMap.Property(e => e.LastName).HasColumnName("lname"); eeMap.Property(e => e.JobID).HasColumnName("job_id"); // job var jobMap = modelBuilder.Entity<Job>(); jobMap.Property(j => j.ID).HasColumnName("job_id"); jobMap.Property(j => j.Description).HasColumnName("job_desc"); } public List<Employee> GetManagers() { var qry = this.Employee.Where(x => x.Job.Description.Contains("manager")); Debug.WriteLine(qry.ToString()); return qry.ToList(); // <-- error here when referencing another database! } } } 

   

Ich denke, dass die Antwort noch nein ist, aber es gibt pathe um sie herum.

Der Grund, warum es nein ist, dass EF einen DBContext verwendet und ein Kontext eine Verbindungszeichenfolge hat und eine Verbindungszeichenfolge zu einer database geht.

Hier sind 2 pathe um ihn herum:

  • Verwenden Sie 2 verschiedene Kontexte eins gegen jede database, das bedeutet, dass Sie data an den Client bringen und auf dem Client merge.
  • Verwenden Sie verknüpfte Tabellen in der database, ziehen Sie data durch viewen, so dass EF sieht es als aus einer einzigen database kommen.

In deinem Code sieht es so aus, als würdest du 2 dbcontexts verwenden

Es gibt zwei Möglichkeiten, es zu tun.

Einer ist natürlich, um eine view in einer der databaseen zu erstellen, die die Querdatenbank-Abfrage macht, dann greifen Sie auf das veiw von Ihrem model, wie Sie irgendeine andere view wünschen.

Die andere war es, die gleiche Cross-database-Abfrage-view innerhalb des models selbst zu erstellen, indem sie eine DefiningQuery erstellen. Das ist am ähnlichsten, wie du es mit SQLClient machen würdest. In SQLClient würden Sie die view in T-SQL als Text eines SQLCommand erstellen und dann den Befehl ausführen, um einen dataleser oder eine datatabelle zu erstellen. Hier verwenden Sie das gleiche T-SQL, um eine DefiningQuery zu erstellen, und verknüpfen sie dann mit einer Entität, die Sie manuell erstellen. Es ist ein bisschen Arbeit, aber genau das willst du.

Hier ist ein Link zur Verwendung von DefiningQuerys : http://msdn.microsoft.com/en-us/library/cc982038.aspx .

Wenn Sie das Buch " Programming Entity Framework " von Lerman von O'Reilly haben, gibt es ein gutes Beispiel in Kapitel 16.

So musst du durch ein paar Reifen springen, um zu tun, was du früher mit SQLClient gemacht hast, aber du bekommst die modellierte Entity.

Die Antwort ist immer noch die gleiche. Wenn du eine Cross-database-Abfrage ausführen willst, musst du auf SQL SqlQuery und SqlQuery auf context.Database .

Warnung! mit definingQuerys kann SEHR langsamer sein!

Hier ist ein Beispiel:

Wenn dies die definierende Abfrage ist, dass Sie eine Entität gegen:

 Select C.CustomerID, C.FirstName, C.LastName, G.SalesCatetory From CustomerDatabase.dbo.Customers C Inner Join MarketingDatabase.dbo.CustomerCategories G on G.CustomerID = C.CustomerID 

Dann, wenn Sie eine Auswahl gegen die Entity von CustomerID ausführen, sieht die SQL-Trace so aus:

 Select [Extent1].[CustomerID] as [CustomerID], [Extent1].[FirstName] as [FirstName], [Extent1].[LastName] as [LastName], [Extent1].[SalesCatetory] as [SalesCatetory] From ( Select C.CustomerID, C.FirstName, C.LastName, G.SalesCatetory From CustomerDatabase.dbo.Customers C Inner Join MarketingDatabase.dbo.CustomerCategories G on G.CustomerID = C.CustomerID ) as [Extent1] Where '123456' = [Extent1].[CustomerID] 

SQL server kann diese Abfrage sehr langsam ausführen. Ich hatte einen Fall, ein wenig komplizierter als das obige Beispiel, wo ich den DefiningQuery Text direkt in einem SQl server Management Console Abfragefenster durch Hinzufügen einer where Klausel für den Wert, den ich für wählen wollte. Es läuft in weniger als einer Sekunde. Dann habe ich die SQL Trace von der Auswahl für den gleichen Wert aus der Entity für diese DefiningQuery erstellt und lief die SQL Trace-Abfrage in einem SQL server Abfrage-windows – es dauerte 13 Sekunden!

Also ich denke, dass nur echte Art, Cross-database-Abfragen zu tun ist, um ein veiw auf dem server zu erstellen.