c# - Entity Framework 7 和 SQLite 的组合键

标签 c# wpf entity-framework sqlite

预先感谢您的帮助!

所以我正在开发一个 WPF 应用程序,并使用 SQLite 和 Entity Framework 7.0.0-Beta8 作为我的 ORM 框架。我正在尝试创建一个复合键,以防止多次插入相同的数据。不幸的是,当我做这样的事情时 modelBuilder.Entity<DeviceConfiguration>().HasKey(d => new {d.property1, d.property2})使用 EF Fluent 代码我得到一个 System.NotSupportedException

System.NotSupportedException was unhandled HResult=-2146233067
Message=SQLite cannot support this migration operation.
Source=EntityFramework.Sqlite

导致这个错误的代码在这里。

public class Device
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public Guid Id { get; set; }
   public string DeviceType { get; set; }
   public UInt32 DeviceIdentifier { get; set; }
   public string PreferredConnection { get; set; }

   public List<DeviceConfiguration> DeviceConfiguration { get; set; }
}

public class DeviceConfiguration
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public Guid Id { get; set; }
   public string DeviceType { get; set; }
   public UInt32 DeviceIdentifier { get; set; }
   public string Description { get; set; }
   public string DateTime { get; set; }
   public string Configuration { get; set; }
   public string Reason { get; set; }

   public List<Device> Device { get; set; }
}

public class DeviceContext : DbContext
{
   public DbSet<Device> Devices { get; set; } 
   public DbSet<DeviceConfiguration> DeviceConfigurations { get; set; } 
   public DbSet<PidTuningSet> PidTuningSets { get; set; }

   protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   {
       string databaseFilePath =  "RUI.db";
       try
       {
           databaseFilePath = Path.Combine(
               Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 
               databaseFilePath);
       }
       catch (InvalidOperationException) { }

       optionsBuilder.UseSqlite($"Data source={databaseFilePath}");
   }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
       modelBuilder.Entity<Device>()
           .Property(d => d.DeviceType)
           .IsRequired();
       modelBuilder.Entity<Device>()
           .Property(d => d.DeviceIdentifier)
           .IsRequired();

       modelBuilder
           .Entity<DeviceConfiguration>()
           .HasKey(d => new {d.DeviceIdentifier, d.DeviceType});

       modelBuilder.Entity<DeviceConfiguration>()
           .Property(d => d.Description)
           .IsRequired();
       modelBuilder.Entity<DeviceConfiguration>()
           .Property(d => d.DateTime)
           .IsRequired();
       modelBuilder.Entity<DeviceConfiguration>()
           .Property(d => d.Configuration)
           .IsRequired();
       modelBuilder.Entity<DeviceConfiguration>()
           .Property(d => d.Reason)
           .IsRequired();

       modelBuilder.Entity<PidTuningSet>()
           .Property(d => d.Locked)
           .IsRequired();
       modelBuilder.Entity<PidTuningSet>()
           .Property(d => d.PIDValues)
           .IsRequired();
       modelBuilder.Entity<PidTuningSet>()
           .Property(d => d.Description)
           .IsRequired()
           .HasMaxLength(32);
   }
}

如果我注释掉 modelBuilder.Entity<DeviceConfiguration>().HasKey(d => new {d.DeviceIdentifier, d.DeviceType});代码并创建一个新的迁移。但是,如果我不对它进行注释,创建一个新的迁移并运行它,我会得到上面不支持的异常。我不确定 EF7 是否支持此操作,或者是否有更好的方法来确保数据库中没有任何重复数据。

最佳答案

我通过创建属性并使用 [Key] 标记并连接值来在模型级别创建此键。我不喜欢这种方法,但如果可能的话,我想通过 EF 来完成。

关于c# - Entity Framework 7 和 SQLite 的组合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33632426/

相关文章:

c# - 从守护进程创建新的 Azure SQL Server

c# - 如何暂停for循环,直到DispatcherTimer结束?

javascript - 如果单击按钮,如何停止执行文本框事件更改方法

.net - "Items collection must be empty before using ItemsSource."

c# - ASP .NET 一对一关系 CRUD

entity-framework - Code First 循环引用外键配置

c# - 将 UINT64 编码为 float

c# - 创建一个 MVVM 友好的对话策略

wpf - 在 Windows 7 和 8 触摸设备上部署 Microsoft Surface 2.0 应用程序

c# - 为什么 Entity Framework dbSet.Include 需要这么长时间才能返回?