C
C#2y ago
morry329#

✅ '' does not contain a definition for '' and no accessible extension method '' can be found

I have this data model
public class Employee : Pilot
{
public Employee()
{

}
public int PersonId { get; set; }
public DateTime StartDate { get; set; }
public int BossId { get; set; }

public string Person { get; set; }


}
public class Employee : Pilot
{
public Employee()
{

}
public int PersonId { get; set; }
public DateTime StartDate { get; set; }
public int BossId { get; set; }

public string Person { get; set; }


}
And the error pops out in the OnModelCreating method:
modelBuilder.Entity<Employee>(entity =>
{

entity.HasOne(d => d.Person)
.WithOne(p => p.Employee) /*ERROR, 'string' does not contain a definition for 'Employee' and no accessible extension method 'Employee' accepting a first argument of type 'string' could be found*/
.HasForeignKey<Employee>(d => d.PersonId)
.OnDelete(DeleteBehavior.Restrict)
.HasConstraintName("FK_MI_Employee_PE_Person");
});
modelBuilder.Entity<Employee>(entity =>
{

entity.HasOne(d => d.Person)
.WithOne(p => p.Employee) /*ERROR, 'string' does not contain a definition for 'Employee' and no accessible extension method 'Employee' accepting a first argument of type 'string' could be found*/
.HasForeignKey<Employee>(d => d.PersonId)
.OnDelete(DeleteBehavior.Restrict)
.HasConstraintName("FK_MI_Employee_PE_Person");
});
` I have tried to inject the Employee keys into the neighbouring data model like this, but to no avail
public class Person
{
public Person()
{
//Employee = new HashSet<Employee>();
}
public Pilot Pilot { get; set; }
public string Employee { get; set; }
}
public class Person
{
public Person()
{
//Employee = new HashSet<Employee>();
}
public Pilot Pilot { get; set; }
public string Employee { get; set; }
}
Could anyone point me in the right direction?
2 Replies
ero
ero2y ago
did you want it to be public Person Person { get; set; } in Employee?
morry329#
morry329#OP2y ago
Oh, this comment was actually helpful 🙂 I just got it resolved by changing one line of a code like this:
public class Person
{
public Person()
{
//Mitarbeiter = new HashSet<Mitarbeiter>();
}
public Pilot Pilot { get; set; }
public Employee Employee { get; set; } //I added this line
}
public class Person
{
public Person()
{
//Mitarbeiter = new HashSet<Mitarbeiter>();
}
public Pilot Pilot { get; set; }
public Employee Employee { get; set; } //I added this line
}
The error was the Employee property had the data type of string so it triggered the error. Your comment made me realise this, thanks a lot 🙂
Want results from more Discord servers?
Add your server