I have a problem.I have two classes that I use in Entity Framework. First one is a person Class: With int id, string name and action creation(incl. Foreign Key). Second one is a action class. With id , time and a person(incl. Foreign Key). When I try to migrate this I get the an error that I should use the Fluent Api to create the relation between this. I searched a bit, but it seams to be a case that I couldn't find information about.
public class Person
{
[Key]
public int IdPerson { get; set; }
public string Name{ get; set; }
public int? CreationId { get; set; }
[ForeignKey("CreationId")]
public Action Creation{ get; set; }
}
public class Action
{
[Key]
public int IdAction { get; set; }
[Required]
[Column(TypeName = "DateTime2")]
public DateTime Moment { get; set; }
public int? PersonId { get; set; }
[ForeignKey("PersonId")]
public Person Person { get; set; }
}
The error message is in german:
Das Prinzipalende einer Zuordnung zwischen den Typen 'FunWeb.Models.Core.Person' und 'FunWeb.Models.Core.Aktion' konnte nicht ermittelt werden. Das Prinzipalende dieser Zuordnung muss mithilfe der Fluent-API für Beziehungen oder mithilfe von Datenanmerkungen explizit konfiguriert werden.
But it says that i need to configure the relation by my self.
Sorry for my english and thanks for your help
Edit: I tried:
modelBuilder.Entity<Person>().HasRequired(p => p.Creation).WithOptional(a => a.Person);
But i get the following error:
Das 'ForeignKeyAttribute' der Eigenschaft 'Person' für den Typ 'Models.Core.Action' ist ungültig. Der Fremdschlüsselname 'PersonId' wurde im abhängigen Typ 'Models.Core.Person' nicht gefunden. Der Name-Wert muss eine durch Trennzeichen getrennte Liste mit Namen von Fremdschlüsseleigenschaften sein.
I try to translate: ForeignKeyAttribute of the propaty Person for the type Action is in valid. The ForeignKey 'PersonId' could not by found in "Models.Core.Person". The name value needs to be a seperated list of names for ForeignKeyAttributes.