简体   繁体   English

具有两个外键的实体框架关系

[英]Entity Framework relationship with two foreign keys

I'm having a problem with connecting two tables in FluentAPI. 我在FluentAPI中连接两个表时遇到问题。 It's in fact a mix of FluentAPI and Data Annotations. 实际上,它是FluentAPI和数据注释的混合。 I Looked at this question but It didn't help me. 我看着这个问题,但没有帮助我。 I tried with Index , composed unique keys. 我尝试使用Index ,它由唯一键组成。

Basically Foo is the main table. 基本上, Foo是主表。 Bar table is optional. Bar桌是可选的。 The are connected via two columns. 通过两列连接。 key1 and key2 pairs are unique. key1key2对是唯一的。 Think of it as a parent-child relationship with a restriction that 1 parent can have only 1 child: 将其视为父子关系,但有一个限制,即1个父母只能生育1个孩子:

Data Entities looks like this: 数据实体看起来像这样:

[Table("foo")]
public class Foo
{
    [Key]
    [Column("pk", TypeName = "int")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int FooId { get; set; }

    [Column("key1", TypeName = "int")]
    public int Key1 { get; set; }
    [Column("key2", TypeName = "int")]
    public int Key2 { get; set; }

    public Bar Bar { get; set; }
}

[Table("bar")]
public class Bar
{
    [Key]
    [Column("key1", TypeName = "int", Order = 1)]
    public int Key1 { get; set; }
    [Key]
    [Column("key2", TypeName = "int", Order = 2)]
    public int Key2 { get; set; }

    public Foo Foo { get; set; }
}    

Here's how I was trying to connect them: 这是我尝试连接它们的方式:

modelBuilder.Entity<Bar>().HasRequired(p => p.Foo).WithOptional(p => p.Bar);

What is wrong? 怎么了? Bar DOES require Foo . Bar确实需要Foo Foo DOES have optional Bar . Foo有可选的Bar <--- this should be totally enough, because Foo has columns named exactly like primary keys in Bar . <---这应该足够了,因为FooBar具有与主键完全相同的命名方式。 But it doesn't work. 但这是行不通的。

So I tried specifying foreign keys: 所以我尝试指定外键:

modelBuilder.Entity<Bar>().HasRequired(p => p.Foo).WithOptional(p => p.Bar).Map(p => p.MapKey(new[] { "key1", "key2" }));

It says: 它说:

"The number of columns specified must match the number of primary key columns" “指定的列数必须与主键列数匹配”

Whaaaat? 哇啊 how? 怎么样? how come? 怎么会? Uhh.. 嗯..

I also tried: 我也尝试过:

modelBuilder.Entity<Bar>().HasIndex(table => new { table.Key1, table.Key2 });  

So my questions are: 所以我的问题是:

  1. Why my solution doesn't work? 为什么我的解决方案不起作用? I do have complex key specified 我确实指定了复杂的密钥

  2. How can I slove it? 我该怎么打呢?

This is going to be a little complicated, and I might be completely wrong here, but from my experience EntityFramework relations don't work that way. 这将有点复杂,在这里我可能完全错了,但是从我的经验来看,EntityFramework关系并不是那样。 It seems to me that if Foo is required and Bar is optional , then each Bar should have a way to join back to Foo uniquely based upon Foo's pk value. 在我看来,如果Foo是必需的,而Bar是可选的 ,则每个Bar应该有一种基于Foo的pk值唯一地返回到Foo的方法。

That is to say that Bar should be defined as: 也就是说,Bar应该定义为:

[Table("bar")]
public class Bar
{
    [Key]
    [Column("key1", TypeName = "int", Order = 1)]
    public int Key1 { get; set; }
    [Key]
    [Column("key2", TypeName = "int", Order = 2)]
    public int Key2 { get; set; }
    public int FooId { get; set; }

    public Foo Foo { get; set; }
}    

You would then need to use this FooId in your description of the relationship, not the composite key contained in Bar. 然后,您需要在关系描述中使用此FooId,而不是Bar中包含的组合键。 EntityFramework has always required me to join on the entire primary key of the parent POCO, which must be a foreign key of the child POCO. EntityFramework一直要求我加入父POCO的整个主键,该主键必须是子POCO的外键。 You may still be able to join through the child's key in LINQ queries. 您可能仍然可以通过子键在LINQ查询中加入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM