实体框架核心导航属性错误

时间:2018-08-19 15:30:51

标签: c# .net entity-framework entity-framework-core

我正在尝试制作一个简单的应用程序来尝试Entity Framework Core,但是我在设置实体之间的关系时遇到了问题。我的实体:

public class Card
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Adress { get; set; }
    public DateTime DoB { get; set; }
    public DateTime DoS { get; set; }
    public User Portal { get; set; }
    public List<Reservation> Res { get; set; }
}
public class Doctor
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public TimeSpan Start_Working { get; set; }
    public TimeSpan End_Working { get; set; }
    public List<Reservation> Reservations { get; set; }
    public int SpecID { get; set; }
    public Spec Spec { get; set; }
}
public class Reservation
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public DateTime DoR { get; set; }
    public string Info { get; set; }
    public int CardID { get; set; }
    public Card Card_Nav_R { get; set; }
    public int DoctorID { get; set; }
    public Doctor Doctor { get; set; }
}
public class Spec
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Doctor> Doctors { get; set; }
}
public class User
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public int CardID { get; set; }
    public Card Card { get; set; }
}

还有一个我试图建立关系的配置类:

class ApplicationContext:DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Card> Cards { get; set; }
    public DbSet<Reservation> Reservations { get; set; }
    public DbSet<Doctor> Doctors { get; set; }
    public DbSet<Spec> Specs { get; set; }

    public ApplicationContext()
    {
        Database.EnsureCreated();
    }
    protected override void OnModelCreating(ModelBuilder ModelBuilder)
    {
          ModelBuilder.Entity<User>().HasKey(u => u.Id);
          ModelBuilder.Entity<Card>().HasKey(c => c.Id);
          ModelBuilder.Entity<Doctor>().HasKey(d => d.Id);
          ModelBuilder.Entity<Spec>().HasKey(s => s.Id);
          ModelBuilder.Entity<Reservation>().HasKey(r => r.Id);

          ModelBuilder.Entity<User>().Property(u => u.Email).IsRequired();
          ModelBuilder.Entity<User>().Property(u => u.Password).IsRequired();

          ModelBuilder.Entity<Card>().Property(c => c.Name).IsRequired();
          ModelBuilder.Entity<Card>().Property(c => c.Surname).IsRequired();
          ModelBuilder.Entity<Card>().Property(c => c.DoB).IsRequired();
          ModelBuilder.Entity<Card>().Property(c => c.Adress).IsRequired();

          ModelBuilder.Entity<Doctor>().Property(d => d.Name).IsRequired();
          ModelBuilder.Entity<Doctor>().Property(d => d.Surname).IsRequired();
          ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();
          ModelBuilder.Entity<Doctor>().Property(d => d.Email).IsRequired();
          ModelBuilder.Entity<Doctor>().Property(d => d.Start_Working).IsRequired();
          ModelBuilder.Entity<Doctor>().Property(d => d.End_Working).IsRequired();

          ModelBuilder.Entity<Reservation>().Property(r => r.Info).IsRequired();
          ModelBuilder.Entity<Reservation>().Property(r => r.Card_Nav_R).IsRequired();
          ModelBuilder.Entity<Reservation>().Property(r => r.Doctor).IsRequired();
          ModelBuilder.Entity<Reservation>().Property(r => r.DoR).IsRequired();

        ModelBuilder.Entity<Spec>().Property(s => s.Name).IsRequired();

          ModelBuilder.Entity<Doctor>().HasOne<Spec>(d=>d.Spec).WithMany(s => s.Doctors).HasForeignKey(d => d.SpecID);
          ModelBuilder.Entity<User>().HasOne<Card>(u => u.Card).WithOne(c => c.Portal).HasForeignKey<User>(u => u.CardID);
          ModelBuilder.Entity<Reservation>().HasOne<Card>(r => r.Card_Nav_R).WithMany(c => c.Res).HasForeignKey(r => r.CardID);
          ModelBuilder.Entity<Reservation>().HasOne<Doctor>(r => r.Doctor).WithMany(d => d.Reservations).HasForeignKey(r => r.DoctorID); 

    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Simple_Try;Trusted_Connection=True;");
    }
}

因此,当我尝试向数据库中添加迁移或添加某些内容时,我看到了此错误:

  

System.InvalidOperationException:无法将属性或导航“ Spec”添加到实体类型“ Doctor”,因为在实体类型“ Doctor”上已经存在具有相同名称的属性或导航。”

我真的不知道如何解决此问题,我尝试使用批注而不是Fluent API,但结果相同。

1 个答案:

答案 0 :(得分:1)

导致异常的原因是以下行:

ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();

因为Doctor.Spec是导航属性

public class Doctor
{
    // ...
    public Spec Spec { get; set; }
}

并且无法通过Property流利的API配置导航属性。

因此只需删除该行。通过关系配置控制参考导航属性是必需的还是可选的。在这种情况下

ModelBuilder.Entity<Doctor>()
   .HasOne(d => d.Spec)
   .WithMany(s => s.Doctors)
   .HasForeignKey(d => d.SpecID)
   .IsRequired(); // <--

尽管IsRequired是自动从FK属性类型派生的-由于SpecID不可为空,因此需要这种关系。

有关更多信息,请参见Required and Optional PropertiesRequired and Optional Relationships文档主题。