将新字段离子添加到我的绑定对象时,DbContext 因 InvalidOperationException 而失败
DbContext failed with InvalidOperationException when add new field ionto my binding object
所以这是我的 binding
类型:
public class ClipboardItem : INotifyPropertyChanged
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
private int id { get; set; }
private string text;
private string name;
private string processName;
private string time;
public ClipboardItem(int id, string text, string name, string processName, string time, bool isPinned)
{
this.id = id;
this.text = text;
this.name = "";
this.processName = processName;
this.time = time;
}
}
DbContext
public class ClipboardItemContext : DbContext
{
public DbSet<ClipboardItem> Clipboards { get; set; }
public string DbPath { get; }
public ClipboardItemContext()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = System.IO.Path.Join(path, "clipboards.db");
Database.EnsureCreated();
}
// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source={DbPath}");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ClipboardItem>().HasKey(p => new { p.Id });
}
}
所以这很好用,我可以 add
、read
、update
和 delete
我的 DB
.
项目
现在我想在 binding type
中添加新字段:
private bool isPinned;
我的Ctor
public ClipboardItem(int id, string text, string name, string processName, string time, bool isPinned)
{
this.id = id;
this.text = text;
this.name = "";
this.processName = processName;
this.time = time;
this.isPinned= isPinned;
Properties.Settings.Default.ItemIndex++;
}
在再次 运行 我的应用程序之前,我删除了我的 .db
文件,因为这个新字段,当我尝试启动我的应用程序时,我得到了这个 exception
:
System.InvalidOperationException: 'No suitable constructor was found
for entity type 'ClipboardItem'. The following constructors had
parameters that could not be bound to properties of the entity type:
cannot bind 'isPinned' in 'ClipboardItem(int id, string text, string
name, string processName, string time, bool isPinned)'.'
我的 DB
似乎不接受这个新字段,我也尝试添加不同的 type
例如 int
或 string
但我还是一样错误。
当我删除这个新字段时,我的 .db
创建了我的 application
运行ning.
Sqlite does not have a separate Boolean storage class. Instead,
Boolean values are stored as integers 0 (false) and 1 (true)
只需将您的字段映射为整数,
[Column(TypeName = "INTEGER")]
public bool isPinned;
或者你可以用OnModelCreating
来做,
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ClipboardItem>().HasKey(p => new { p.Id });
modelBuilder.Entity<ClipboardItem>().Property(p => p.isPinned)
.HasConversion(x => x ? 1 : 0,
x => (x == 1));
}
所以这是我的 binding
类型:
public class ClipboardItem : INotifyPropertyChanged
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
private int id { get; set; }
private string text;
private string name;
private string processName;
private string time;
public ClipboardItem(int id, string text, string name, string processName, string time, bool isPinned)
{
this.id = id;
this.text = text;
this.name = "";
this.processName = processName;
this.time = time;
}
}
DbContext
public class ClipboardItemContext : DbContext
{
public DbSet<ClipboardItem> Clipboards { get; set; }
public string DbPath { get; }
public ClipboardItemContext()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = System.IO.Path.Join(path, "clipboards.db");
Database.EnsureCreated();
}
// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source={DbPath}");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ClipboardItem>().HasKey(p => new { p.Id });
}
}
所以这很好用,我可以 add
、read
、update
和 delete
我的 DB
.
现在我想在 binding type
中添加新字段:
private bool isPinned;
我的Ctor
public ClipboardItem(int id, string text, string name, string processName, string time, bool isPinned)
{
this.id = id;
this.text = text;
this.name = "";
this.processName = processName;
this.time = time;
this.isPinned= isPinned;
Properties.Settings.Default.ItemIndex++;
}
在再次 运行 我的应用程序之前,我删除了我的 .db
文件,因为这个新字段,当我尝试启动我的应用程序时,我得到了这个 exception
:
System.InvalidOperationException: 'No suitable constructor was found for entity type 'ClipboardItem'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'isPinned' in 'ClipboardItem(int id, string text, string name, string processName, string time, bool isPinned)'.'
我的 DB
似乎不接受这个新字段,我也尝试添加不同的 type
例如 int
或 string
但我还是一样错误。
当我删除这个新字段时,我的 .db
创建了我的 application
运行ning.
Sqlite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true)
只需将您的字段映射为整数,
[Column(TypeName = "INTEGER")]
public bool isPinned;
或者你可以用OnModelCreating
来做,
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ClipboardItem>().HasKey(p => new { p.Id });
modelBuilder.Entity<ClipboardItem>().Property(p => p.isPinned)
.HasConversion(x => x ? 1 : 0,
x => (x == 1));
}