简体   繁体   English

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

[英]Entity Framework Core Navigation Properties Error

I'm trying to make a simple app to try Entity Framework Core, but ia have problem with setting up relations between entities. 我正在尝试制作一个简单的应用程序来尝试Entity Framework Core,但是我在设置实体之间的关系时遇到了问题。 My entities: 我的实体:

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; }
}

And a configuration class where i tried to set up relations: 还有一个我试图建立关系的配置类:

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;");
    }
}

So, when i tried to add migration or add something to database i saw this error: 因此,当我尝试添加迁移或向数据库中添加某些内容时,我看到了此错误:

System.InvalidOperationException: 'The property or navigation 'Spec' cannot be added to the entity type 'Doctor' because a property or navigation with the same name already exists on entity type 'Doctor'.' System.InvalidOperationException:无法将属性或导航“ Spec”添加到实体类型“ Doctor”,因为在实体类型“ Doctor”上已经存在具有相同名称的属性或导航。

I really don't know how to fix this, i tried to use annotations instead of Fluent API, but had the same result. 我真的不知道如何解决此问题,我尝试使用注释而不是Fluent API,但结果相同。

The cause of the exception is the following line: 导致异常的原因是以下几行:

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

because Doctor.Spec is a navigation property 因为Doctor.Spec是导航属性

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

and navigation properties cannot be configured via Property fluent API. 并且无法通过Property fluent API配置导航属性。

So simply remove that line. 因此,只需删除该行。 Whether reference navigation property is required or optional is controlled via relationship configuration. 通过关系配置控制参考导航属性是必需的还是可选的。 In this case 在这种情况下

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

although the IsRequired is automatically derived from the FK property type - since SpecID is non nullable, then the relationship is required. 尽管IsRequired是自动从FK属性类型派生的-由于SpecID不可为空,因此该关系是必需的。

For more info, see Required and Optional Properties and Required and Optional Relationships documentation topics. 有关更多信息,请参见必需和可选属性以及必需和可选关系文档主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM