在EntityFramework, Version=6.0.0.0,维护一个现有项目,修改代码后,编译正常。但运行系统,竟然报发生异常,加断点跟踪发现抛出“One or more validation errors were detected during model generation”
由于对EF不够熟悉。所以网上搜索了一番,提示:原因是因为我在写实体类的时候没有为实体类中的属性声明一个主键,即用[key]特性标注在属性上,这样DbContext才能为我们在数据库上找到对应的主键。由于是现有项目进行的修改,我找到自己的实体类,和之前的类对比,并没有这个属性。
又查了一些资料如下:
而在EF模型中的主键有如下规则:
Id
或者类名Id
这个属性设置为主键。Id
和类名Id
约定例子:
class Car { public string CarId { get; set; }//主键 public string Make { get; set; } public string Model { get; set; } }
class Car { [Key] public string LicensePlate { get; set; } public string Make { get; set; } public string Model { get; set; } }
OnModelCreating方法
)class MyContext : DbContext
{
public DbSet<Car> Cars { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.HasKey(c => c.LicensePlate);
}
}
class Car
{
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
OnModelCreating方法
)class MyContext : DbContext
{
public DbSet<Car> Cars { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.HasKey(c => new { c.State, c.LicensePlate });
}
}
class Car
{
public string State { get; set; }
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
在OnModelCreating方法中,如果有很多实体的话,OnModelCreating方法管理很麻烦,可以通过继承System.Data.Entity.ModelConfiguration命名空间中的
EntityTypeConfiguration<TEntity>类来实现
public class xxx: EntityTypeConfiguration<Student> { public xxx() { this.ToTable("StudentInfo"); this.HasKey<int>(s => s.StudentKey); } }
One or more validation errors were detected during model generation(在模型生成期间检测到一个或多个验证错误)
原文:https://www.cnblogs.com/gougou1981/p/12291733.html