在实体框架中设置多个外键作为主键

时间:2022-01-25 11:29:55

I'm using entity framework to manage my sql-server-ce database. i want my table's primary key to be consisted of several foreign keys to other tables. i expected something like this to work:

我正在使用实体框架来管理我的sql-server-ce数据库。我希望我的表的主键由其他表的几个外键组成。我原以为这样的东西会有用:

class Bill{
    [Key]
    public virtual Customer Customer { get; set; }
    [Key]
    public virtual Era Era { get; set; }
    [Key]
    public virtual CompanyCode CompanyCode { get; set; }
    public long? Amount { get; set; }
}

but it results in the following database migration error:

但这会导致以下数据库迁移错误:

BillPrinter.Bill: : EntityType 'Bill' has no key defined. Define the key for this EntityType. Bills: EntityType: EntitySet 'Bills' is based on type 'Bill' that has no keys defined.

BillPrinter。Bill:: EntityType 'Bill'没有定义键。定义此实体类型的键。Bill: EntityType: EntitySet 'Bills'是基于没有定义键的'Bill'类型。

how can i make my table have a primary key consisted of those three foreign keys?

如何使我的表有一个主键由这三个外键组成?

1 个解决方案

#1


5  

You can't use navigation properties as PKs. Navigation properties provide a way to navigate an association between two entity types but they don't represent by themselves the FK of the relationship. You need to declare explicitly three additional properties to represent the FKs of your relationships, like in this model:

不能将导航属性用作PKs。导航属性提供了一种方法来导航两个实体类型之间的关联,但是它们本身并不表示关系的FK。您需要显式声明三个额外的属性来表示您的关系的FKs,就像在这个模型中一样:

public class Customer
{
  public int Id {get;set;}
  //...
}

public class Era 
{
  public int Id {get;set;}
  //...
}

public class CompanyCode 
{
  public int Id {get;set;}
  //...
}


public class Bill
{
  [Key] 
  [Column(Order=1)] 
  [ForeignKey("Customer")]
  public int CustomerId {get;set;}

  [Key] 
  [Column(Order=2)] 
  [ForeignKey("Era")]
  public int EraId {get;set;}


  [Key] 
  [Column(Order=3)] 
  [ForeignKey("CompanyCode")]
  public int CompanyCodeId {get;set;}
  //...
  public virtual Customer Customer { get; set; }
  public virtual Era Era { get; set; }
  public virtual CompanyCode CompanyCode { get; set; }
}

As you can see, when you have composite keys, Entity Framework requires you to define an order of the key properties. You can do this using the Column annotation to specify an order. Also, you need to use the ForeignKey data annotation to clarify your intention which navigation property represents the relationship it is a foreign key for.

如您所见,当您拥有复合键时,实体框架要求您定义键属性的顺序。您可以使用列注释来指定顺序。此外,您还需要使用ForeignKey数据注释来澄清您的意图:哪个导航属性表示它作为外键的关系。

#1


5  

You can't use navigation properties as PKs. Navigation properties provide a way to navigate an association between two entity types but they don't represent by themselves the FK of the relationship. You need to declare explicitly three additional properties to represent the FKs of your relationships, like in this model:

不能将导航属性用作PKs。导航属性提供了一种方法来导航两个实体类型之间的关联,但是它们本身并不表示关系的FK。您需要显式声明三个额外的属性来表示您的关系的FKs,就像在这个模型中一样:

public class Customer
{
  public int Id {get;set;}
  //...
}

public class Era 
{
  public int Id {get;set;}
  //...
}

public class CompanyCode 
{
  public int Id {get;set;}
  //...
}


public class Bill
{
  [Key] 
  [Column(Order=1)] 
  [ForeignKey("Customer")]
  public int CustomerId {get;set;}

  [Key] 
  [Column(Order=2)] 
  [ForeignKey("Era")]
  public int EraId {get;set;}


  [Key] 
  [Column(Order=3)] 
  [ForeignKey("CompanyCode")]
  public int CompanyCodeId {get;set;}
  //...
  public virtual Customer Customer { get; set; }
  public virtual Era Era { get; set; }
  public virtual CompanyCode CompanyCode { get; set; }
}

As you can see, when you have composite keys, Entity Framework requires you to define an order of the key properties. You can do this using the Column annotation to specify an order. Also, you need to use the ForeignKey data annotation to clarify your intention which navigation property represents the relationship it is a foreign key for.

如您所见,当您拥有复合键时,实体框架要求您定义键属性的顺序。您可以使用列注释来指定顺序。此外,您还需要使用ForeignKey数据注释来澄清您的意图:哪个导航属性表示它作为外键的关系。