Вопрос

I want to know if there is a way to achieve a related list of the same entity on the same class or use migrations to setup a model with this attribute in it?

public class Person
{
    [Key]
    public Guid Id { get; set; }

    public string Name { get; set; }

    [InverseProperty("FamilyMembers")]
    public List<Person> FamilyMembers { get; set; }
}

At the moment when I am using migrations (AddMigration) to setup the database I get the following exception:

Add-Migration : A relationship cannot be established from property 'FamilyMembers' on type 'ConsoleApplication3.Person' to property 'FamilyMembers' on type 'ConsoleApplication3.Person'. Check the values in the InversePropertyAttribute to ensure relationship definitions are unique and reference from one navigation property to its corresponding inverse navigation property.

Also I get the same error adding Person's to the People dbset when running the a test app.

        var p1 = new Person();
        p1.Id = Guid.NewGuid();
        p1.Name = "p1";

        var p2 = new Person();
        p2.Id = Guid.NewGuid();
        p2.Name = "p2";

        var c = new TestContext();
        c.People.Add(p1);

Is there another attribute that is not unique and references the same navigation property?

Это было полезно?

Решение

The simple answer is no. Each end of the relation must have its own navigation property.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top