Entity Framework 核心 2.1:无法在实体类型 'BarCodeDevice' 上为 属性 'Model' 找到指定的字段 'Model'
Entity Framework Core 2.1 : The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'
我需要使用 Code First 方法通过 Entity Framework Core 2.1 生成数据库,但出现此错误:
The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'.
这是我曾经这样做过的 classes
public class BarCodeDevice
{
public int SerialNumber { get; set; }
public string Model { get; set; }
public virtual ICollection<ClientBarCodeDevice> ClientBarCodeDeviceList { get; set; }
}
和配置class
public class BarCodeDeviceConfiguration : IEntityTypeConfiguration<BarCodeDevice>
{
public void Configure(EntityTypeBuilder<BarCodeDevice> builder)
{
builder.HasKey(x => x.SerialNumber);
builder.Property(t => t.Model)
.IsRequired()
.HasField("Model");
}
}
和 DbContext Class
public class SegregationDbContext : DbContext, IDisposable
{
public SegregationDbContext(DbContextOptions<SegregationDbContext> options) : base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BarCodeDeviceConfiguration());
}
public DbSet<BarCodeDevice> BarCodeDevices { get; set; }
}
最后是配置
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SegregationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc();
}
问题是这个流畅的配置行:
.HasField("Model")
HasField
用于指定backing field for the property being configured, when the backing field name does not conform to the conventions.
但是你 Model
属性 是自动 属性 并且没有名为 Model
的支持字段,因此例外。
所以要么删除该行,例如
builder.Property(t => t.Model)
.IsRequired();
或者如果您想强制使用名称未知的支持字段(自动属性就是这种情况),请改用 UsePropertyAccessMode
方法,例如
builder.Property(t => t.Model)
.IsRequired()
.UsePropertyAccessMode(PropertyAccessMode.Field);
我需要使用 Code First 方法通过 Entity Framework Core 2.1 生成数据库,但出现此错误:
The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'.
这是我曾经这样做过的 classes
public class BarCodeDevice
{
public int SerialNumber { get; set; }
public string Model { get; set; }
public virtual ICollection<ClientBarCodeDevice> ClientBarCodeDeviceList { get; set; }
}
和配置class
public class BarCodeDeviceConfiguration : IEntityTypeConfiguration<BarCodeDevice>
{
public void Configure(EntityTypeBuilder<BarCodeDevice> builder)
{
builder.HasKey(x => x.SerialNumber);
builder.Property(t => t.Model)
.IsRequired()
.HasField("Model");
}
}
和 DbContext Class
public class SegregationDbContext : DbContext, IDisposable
{
public SegregationDbContext(DbContextOptions<SegregationDbContext> options) : base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BarCodeDeviceConfiguration());
}
public DbSet<BarCodeDevice> BarCodeDevices { get; set; }
}
最后是配置
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SegregationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc();
}
问题是这个流畅的配置行:
.HasField("Model")
HasField
用于指定backing field for the property being configured, when the backing field name does not conform to the conventions.
但是你 Model
属性 是自动 属性 并且没有名为 Model
的支持字段,因此例外。
所以要么删除该行,例如
builder.Property(t => t.Model)
.IsRequired();
或者如果您想强制使用名称未知的支持字段(自动属性就是这种情况),请改用 UsePropertyAccessMode
方法,例如
builder.Property(t => t.Model)
.IsRequired()
.UsePropertyAccessMode(PropertyAccessMode.Field);