我正在使用Entity Framework 5 code first
. 我正在尝试在 2 个表之间设置内部连接,但我不知道该怎么做。这两个表没有主键/外键关联,但它们有一个公共字段域。
tblServer 表:
Server_ID
ServerName
Domain
...
...
tblCommandExecutionServer 表:
ServerListID
ServerName
Domain
...
...
这就是我配置 2 个表以映射到某些实体类的方式:
服务器配置类:
class ServerConfiguration : EntityTypeConfiguration<Server>
{
internal ServerConfiguration()
{
this.ToTable("tblServer");
this.Property(x => x.Id).HasColumnName("Server_ID");
this.Property(x => x.Name).HasColumnName("ServerName");
}
}
CommandExecutionServerConfiguration 类:
class CommandExecutionServerConfiguration : EntityTypeConfiguration<CommandExecutionServer>
{
internal CommandExecutionServerConfiguration()
{
this.ToTable("tblCommandExecutionServer");
this.Property(x => x.Id).HasColumnName("ServerListID");
this.Property(x => x.Name).HasColumnName("ServerName");
}
}
服务器类:
public class Server : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Domain { get; set; }
public virtual CommandExecutionServer CommandExecutionServer { get; set; }
}
命令执行服务器类:
public class CommandExecutionServer : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public string Domain { get; set; }
public virtual Server Server { get; set; }
}
这两个表没有通过任何列链接。我必须创建以下内部联接,但不确定如何:
SELECT
ces.ServerName,
ws.ServerName,
ws.Domain
FROM
tblServer ws
INNER JOIN tblCommandExecutionServer ces ON ws.Domain = ces.Domain
WHERE
ws.ServerName = 'my-server-name' AND ces.Active = 1;
我的数据库上下文类:
public DbSet<Server> Servers { get; set; }
public DbSet<CommandExecutionServer> CommandExecutionServers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ServerConfiguration());
modelBuilder.Configurations.Add(new CommandExecutionServerConfiguration());
}
然后这就是我目前拥有的,我不知道这应该是什么样子?
public Server FindByServerName(string server, bool isActive, string domain)
{
return DatabaseContext.Servers
.SingleOrDefault(entity => entity.Name == server
&& entity.IsActive == isActive);
//&& entity.Domain == server
}