我有一个桌面联系人,有两部手机可用于主电话和第二部电话。这两个都有字段primaryphonetypeid和secondaryphonetypeid。这两列引用了同一个表的phonetype。无论我做什么,EF都不会将secondaryphonetypeid识别为外键。我甚至从我的代码中删除了primaryphonetypeid并杀死了它的外键约束,它仍然说secondaryphonetypeid不存在。
[Table("PhoneType", Schema = "lkp")]
public class PhoneTypeEntity
{
[Key]
[Required]
[Column("PhoneTypeId")]
public int PhoneTypeId { get; set; }
[Column("PhoneTypeName")]
public string PhoneTypeName { get; set; }
}
public class ContactEntity
{
[Key]
[Required]
[Column("ContactId")]
public int ContactId { get; set; }
[Column("AccountId")]
public int? AccountId { get; set; }
[ForeignKey("AccountId")]
public virtual AccountEntity Account { get; set; }
[Column("ContactTypeId")]
public int? ContactTypeId { get; set; }
[ForeignKey("ContactTypeId")]
public virtual ContactTypeEntity ContactType { get; set; }
[Column("FirstName")]
public string FirstName { get; set; }
[Column("LastName")]
public string LastName { get; set; }
[Column("EmailAddress")]
public string EmailAddress { get; set; }
[Column("PrimaryPhone")]
public string PrimaryPhone { get; set; }
[Column("PreferredContactMethodId")]
public int? PreferredContactMethodId { get; set; }
[ForeignKey("PreferredContactMethodId")]
public virtual PreferredContactMethodEntity PreferredContactMethod { get; set; }
[Column("AddressLine1")]
public string AddressLine1 { get; set; }
[Column("AddressLine2")]
public string AddressLine2 { get; set; }
[Column("PrimaryPhoneTypeId")]
public int? PrimaryPhoneTypeId { get; set; }
[ForeignKey("PrimaryPhoneTypeId")]
public virtual PhoneTypeEntity PrimaryPhoneType { get; set; }
[Column("SecondaryPhone")]
public string SecondaryPhone { get; set; }
[Column("SecondaryPhoneTypeId")]
public int? SecondayPhoneTypeId { get; set; }
[ForeignKey("SecondaryPhoneTypeId")]
public virtual PhoneTypeEntity SecondaryPhoneType { get; set; }
}
正如您所看到的,此表中还有其他外键,它们都可以正常工作。
这是包含约束的表。无论我尝试过什么,它都不会将SecondaryPhoneTypeId识别为实体框架中的外键。
CREATE TABLE [dbo].[Contact](
[ContactId] [int] IDENTITY(1,1) NOT NULL,
[AccountId] [int] NULL,
[ContactTypeId] [int] NOT NULL,
[PreferredContactMethodId] [int] NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[AddressLine1] [nvarchar](100) NULL,
[AddressLine2] [nvarchar](100) NULL,
[PrimaryPhone] [nvarchar](10) NULL,
[PrimaryPhoneTypeId] [int] NULL,
[SecondaryPhone] [nvarchar](10) NULL,
[SecondaryPhoneTypeId] [int] NULL,
[State] [nvarchar](5) NULL,
[EmailAddress] [nvarchar](100) NULL,
[PostalCode] [nvarchar](10) NULL,
[IsMilitary] [bit] NULL DEFAULT ((0)),
[City] [nvarchar](50) NULL,
CONSTRAINT [PK_ContactDetail] PRIMARY KEY CLUSTERED
(
[ContactId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Contact] WITH NOCHECK ADD CONSTRAINT [FK_Contact_Account] FOREIGN KEY([AccountId])
REFERENCES [dbo].[Account] ([AccountId])
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[Contact] NOCHECK CONSTRAINT [FK_Contact_Account]
GO
ALTER TABLE [dbo].[Contact] WITH CHECK ADD CONSTRAINT [FK_Contact_ContactType] FOREIGN KEY([ContactTypeId])
REFERENCES [lkp].[ContactType] ([ContactTypeId])
GO
ALTER TABLE [dbo].[Contact] CHECK CONSTRAINT [FK_Contact_ContactType]
GO
ALTER TABLE [dbo].[Contact] WITH NOCHECK ADD CONSTRAINT [FK_Contact_PreferredContactMethod] FOREIGN KEY([PreferredContactMethodId])
REFERENCES [lkp].[PreferredContactMethod] ([PreferredContactMethodId])
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[Contact] NOCHECK CONSTRAINT [FK_Contact_PreferredContactMethod]
GO
ALTER TABLE [dbo].[Contact] WITH NOCHECK ADD CONSTRAINT [FK_Contact_PrimaryPhoneTypeId] FOREIGN KEY([PrimaryPhoneTypeId])
REFERENCES [lkp].[PhoneType] ([PhoneTypeId])
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[Contact] NOCHECK CONSTRAINT [FK_Contact_PrimaryPhoneTypeId]
GO
ALTER TABLE [dbo].[Contact] WITH NOCHECK ADD CONSTRAINT [FK_Contact_SecondaryPhoneTypeId] FOREIGN KEY([SecondaryPhoneTypeId])
REFERENCES [lkp].[PhoneType] ([PhoneTypeId])
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[Contact] NOCHECK CONSTRAINT [FK_Contact_SecondaryPhoneTypeId]
GO
enter code here