如何在EF 6中设置索引和外键

时间:2018-04-23 16:51:14

标签: c# entity-framework entity-framework-6

更新

我放弃了桌子并将表格添加到建议中,现在看起来不错,所以请查看更新的内容。

尝试定义表约束时会产生以下错误ALTER TABLE ALTER COLUMN failed because column 'Id' does not exist in table 'Region'.

我假设迁移脚本(下面)中的这个语句RenameColumn(table: "dbo.Region", name: "Id", newName: "OrganizationId");是根,但我不明白为什么要将Region.Id(PK)重命名为{{1} } ...我猜是因为org id被多次使用而且它正在绊倒魔法ef配置......?

这是源表(具有正确的约束):

OrganizationId

模型配置:

CREATE TABLE [dbo].[Region]
(
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [OrganizationID] [int] NOT NULL,
      [Name] [nvarchar](255) NOT NULL,
      [ParentRegionID] [int] NULL,

      CONSTRAINT [PK_Region] 
          PRIMARY KEY CLUSTERED ([Id] ASC),
      CONSTRAINT [uqc_Region_OrganizationID] 
          UNIQUE NONCLUSTERED ([Id] ASC, [OrganizationID] ASC)
 ) ON [PRIMARY]
 GO

 ALTER TABLE [dbo].[Region]  WITH CHECK 
     ADD CONSTRAINT [Organization_Region_fk] 
     FOREIGN KEY([OrganizationID]) REFERENCES [dbo].[Organization] ([Id])
 GO

 ALTER TABLE [dbo].[Region] CHECK CONSTRAINT [Organization_Region_fk]
 GO

实体类:

        modelBuilder.Entity<Region>()
            .HasKey(k => k.Id);

        modelBuilder.Entity<Region>()
            .Property(i => i.Id)
            .HasColumnAnnotation(
                IndexAnnotation.AnnotationName,
                new IndexAnnotation(new IndexAttribute("uqc_Region_OrganizationID", 1)));

        modelBuilder.Entity<Region>()
            .Property(o => o.OrganizationId)
            .HasColumnAnnotation(
                IndexAnnotation.AnnotationName,
                new IndexAnnotation(new IndexAttribute("uqc_Region_OrganizationID", 2)));

        modelBuilder.Entity<Region>()
            .HasRequired(o => o.Organization)
            .WithMany()
            .HasForeignKey(o => o.OrganizationId);

迁移脚本:

public class EntityBase
{
    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(255)]
    public virtual string Name { get; set; }
}

public class Region : EntityBase
{
    public int OrganizationId { get; set; }
    public int ParentRegionId { get; set; }
    public Organization Organization { get; set; }
    public Region ParentRegion { get; set; }
}

public class Organization : EntityBase
{
    public int? ParentOrgId { get; set; }
    public int? OrganizationTypeId { get; set; }
    public int TimeZoneId { get; set; }

    public virtual ICollection<ApplicationEmployeeMap> ApplicationEmployeeMaps { get; set; } = new HashSet<ApplicationEmployeeMap>();

    public virtual ICollection<Facility> Facilities { get; set; } = new HashSet<Facility>();

    public virtual ICollection<Employee> Employees { get; set; } = new HashSet<Employee>();

    public virtual ICollection<Organization> Organization1 { get; set; } = new HashSet<Organization>();

    public virtual Organization Organization2 { get; set; }
}

2 个答案:

答案 0 :(得分:1)

我不知道为什么

但它应该是地区和组织之间的正确地图

        modelBuilder.Entity<Region>()
            .HasRequired(me => me.Organization)
            .WithMany()
            .HasForeignKey(me => me.OrganizationId); //FK property

答案 1 :(得分:1)

看起来这段代码正在构建额外的外键,将其视为自引用外键

modelBuilder.Entity<Region>()
        .HasRequired(p => p.ParentRegion)
        .WithRequiredDependent();