【问题标题】:.Net Entity Framework Core Unique index with Foreign Key.Net Entity Framework Core 具有外键的唯一索引
【发布时间】:2020-07-08 09:41:49
【问题描述】:

问题: 嗨,大家好。我使用的是 EF core 2.1,我需要在表 InstallationInformation 中将属性 LicenceKey 和 Importrun(FK) 设置为唯一索引(组合)。

添加信息: 如果我对我所做的研究是正确的。我不能在 .net Core 2.1 中使用 Annotation [index],因为它不受支持。我尝试升级,但它需要我没有的 VS 2019。并且要使用 VS 2019,我需要我也没有使用的 windows 10。

我尝试过的方法。 我尝试了注释。但不幸的是,它不起作用。 我发现我可以尝试使用 Fluent API 并发现我可以映射属性。

modelBuilder.Entity<InstallationInformation>(entity =>
            {
                entity.HasIndex(i => new { i.LicenceKey, i.ImportRun }).IsUnique();
            });

但是当我调试时它出现了这个错误 “InvalidOperationException:‘ImportRun’不能用作实体类型‘InstallationInformation’的属性,因为它被配置为导航。”

代码结构:

public class ImportRun
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ImportRunId { get; set; }

        public DateTime ExtractedDate { get; set; }

        public DateTime ProcessDate { get; set; }

        public ResultCondition ProcessResult { get; set; }        

        public string FailureDescription { get; set; }

        [Required]
        public virtual ICollection<InstallationInformation> InstallationInformation { get; set; }

    }
public class InstallationInformation
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int InstallationInformationID { get; set; }

        [MaxLength(256)]
        public string LicenceKey { get; set; }

        [MaxLength(256)]
        public string ProductVersion { get; set; }

        [MaxLength(256)]
        public string ProductName { get; set; }        

        [MaxLength(256)]
        public string CompanyName { get; set; }

        public DateTime Timestamp { get; set; }
        [Required]
        public ImportRun ImportRun { get; set; }
    }
protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<ImportRun>()
                .HasMany(c => c.InstallationInformation)
                .WithOne(e => e.ImportRun)
                .IsRequired();

            modelBuilder.Entity<InstallationInformation>(entity =>
            {
                entity.HasIndex(i => new { i.LicenceKey, i.ImportRun }).IsUnique();
            });
        }

【问题讨论】:

    标签: c# entity-framework-core


    【解决方案1】:

    变化:

                modelBuilder.Entity<InstallationInformation>(entity =>
                {
                    entity.HasIndex(i => new { i.LicenceKey, i.ImportRun }).IsUnique();
                });
    

    收件人:

                modelBuilder.Entity<InstallationInformation>(entity =>
                {
                    entity.HasIndex(i => new { i.LicenceKey, i.ImportRunId }).IsUnique();
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-03
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2016-12-16
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多