复合键和外键
我正在开发 asp.net MVC3 应用程序,我有以下实体
该实体具有复合键(CreditRegistryId 和 Accoubnt No 是主键)。 CreditRegistryId 也是一个外键。如何制作复合键和外键。我正在使用 EF 4.1 中的 DbContext API。我没有使用 edmx (ORM 设计器)
[Table("tbaAccount")]
public class Account
{
[Column(Name="Creditor Registry ID")] // PK FK
public int CreditRegistryId {get;set;}
[Column(Name = "[Account No]")] //PK
public int AccountNo { get; set; }
[Column(Name = "[Date Opened]")]
public DateTime DateOpened { get; set; }
[ForeignKey("tblAccountStatus")]
[Required]
[Column(Name = "Account Status ID")] // FK
public int AccountStatusId { get; set; }
[Required]
[Column(Name = "Date First Reported")]
public DateTime DateFirstReported { get; set; }
[Required]
[Column(Name = "Credit Limit")]
public double CreditLimit { get; set; }
[Required]
public double Balance { get; set; }
}
,其他实体可以说是
public class CreditRegistry
{
public int CreditRegistryId {get;set;}
}
I am developing asp.net MVC3 application and I have following entity
This entity has composite key (CreditRegistryId and Accoubnt No are primary keys). CreditRegistryId is a foreign key as well. How can I make composite key and foreign key. I am using DbContext API from EF 4.1. I am not using edmx ( ORM designer)
[Table("tbaAccount")]
public class Account
{
[Column(Name="Creditor Registry ID")] // PK FK
public int CreditRegistryId {get;set;}
[Column(Name = "[Account No]")] //PK
public int AccountNo { get; set; }
[Column(Name = "[Date Opened]")]
public DateTime DateOpened { get; set; }
[ForeignKey("tblAccountStatus")]
[Required]
[Column(Name = "Account Status ID")] // FK
public int AccountStatusId { get; set; }
[Required]
[Column(Name = "Date First Reported")]
public DateTime DateFirstReported { get; set; }
[Required]
[Column(Name = "Credit Limit")]
public double CreditLimit { get; set; }
[Required]
public double Balance { get; set; }
}
and other entity lets say is
public class CreditRegistry
{
public int CreditRegistryId {get;set;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EF 不支持仅使用参与复合键的属性之一的关联。
不确定这个场景是否可以实现。
EF does not support associations that use only one property of the ones that participate in a composite key.
Not sure if this scenario can be implemented.