2

我正在使用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
}
4

1 回答 1

7

It is not supported to map such relation into navigation property. Relation can be mapped only on top of primary key (at least EF must believe that defined column in principal entity is PK). To support your relation in the database Domain in the Server will have to be unique but EF doesn't support unique constraints yet.

You can only use manual linq join to execute similar query:

var query = from s in context.Server
            join c in context.CommandExecutionServer on s.Domain equals c.Domain
            where s.ServerName == server && c.Active == isActive 
            select new {
                c.ServerName,
                s.ServerName,
                s.Domain
            };
于 2013-03-07T07:55:53.033 回答