使用EF6 Code First而不迁移到现有数据库如何指定多对多连接表的列名?

时间:2017-10-19 19:29:01

标签: c# entity-framework

我使用EF6访问一组三个表“TableA”,“TableB”和“TableC” TableA与TableB和TableC有多对多的关系,因此有两个表创建了TableATableB& TableATableC。这些表各有两列,即。 TableA_Id和TableB_Id或TableA_Id和TableC_Id。这有效,但现在DBA要我从这些列中删除下划线字符。

虽然原始表是使用迁移创建的,但我已从项目中删除了迁移方面。我已经在实际的连接表中重命名了列,但是如何让关系回到TableA呢?是否可以通过数据注释或流畅?

表A:

vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
Progress: 90%
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: A newer version of the box 'ubuntu/trusty64' is available! You 
currently
==> default: have version '20171010.0.0'. The latest is version 
'20171012.0.0'. Run
==> default: `vagrant box update` to update.
==> default: Setting the name of the VM: 
VagrantTest_default_1508440636670_47439
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: 
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default: 
default: Inserting generated public key within guest...
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
C:/HashiCorp/Vagrant/embedded/gems/gems/net-scp-1.2.1/lib/net/scp.rb:398:in 
`await_response_state': scp: /tmp/vagrant-insert-pubkey-1508440708: Read-
only file system (RuntimeError)
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-scp-
1.2.1/lib/net/scp.rb:369:in `block (3 levels) in start_command'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-
4.1.0/lib/net/ssh/connection/channel.rb:320:in `process'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:238:in `block in ev_preprocess'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:529:in `each'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:529:in `each_channel'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:238:in `ev_preprocess'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/event_loop.rb:99:in `each'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/event_loop.rb:99:in `ev_preprocess'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/event_loop.rb:27:in `process'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:216:in `process'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:178:in `block in loop'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:178:in `loop'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/session.rb:178:in `loop'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/connection/channel.rb:269:in `wait'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-scp-1.2.1/lib/net/scp.rb:284:in `upload!'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:291:in `block in upload'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:684:in `block in scp_connect'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:332:in `connect'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:682:in `scp_connect'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:285:in `upload'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/guests/linux/cap/public_key.rb:19:in `block in insert_public_key'
from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.3.0/tempfile.rb:295:in `open'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/guests/linux/cap/public_key.rb:14:in `insert_public_key'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/lib/vagrant/capability_host.rb:111:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/lib/vagrant/capability_host.rb:111:in `capability'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/lib/vagrant/guest.rb:43:in `capability'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:187:in `ready?'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:83:in `block in wait_for_ready'
from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.3.0/timeout.rb:91:in `block in timeout'
from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.3.0/timeout.rb:33:in `block in catch'
from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.3.0/timeout.rb:33:in `catch'
from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.3.0/timeout.rb:33:in `catch'
from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.3.0/timeout.rb:106:in `timeout'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/plugins/communicators/ssh/communicator.rb:60:in `wait_for_ready'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.0/lib/vagrant/action/builtin/wait_for_communicator.rb:16:in `block in call'

Process finished with exit code 1

表B:

 public class TableA
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ICollection<TableB> TableBs { get; set; }
    public virtual ICollection<TableC> TableCs { get; set; }
}

表C:

 public class TableB
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<TableA> TableAs { get; set; }
}

这与之前的问题不同,因为它指定已创建连接表并具有特定的列名称。问题是如何让现有代码映射到正确的列

名。

1 个答案:

答案 0 :(得分:2)

我会说流利的,你可以提到下面的键。 为了更好地理解,贝洛是学生和课程的一个例子

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Student>()
                .HasMany<Course>(s => s.Courses)
                .WithMany(c => c.Students)
                .Map(cs =>
                        {
                            cs.MapLeftKey("StudentId");
                            cs.MapRightKey("CourseId");
                            cs.ToTable("StudentCourse"); // this is mappingtable
                        });

}
  

以上示例最好在Blog解释,您可以说博客清楚地解释了多对多的关系