1

I've been searching for similar questions to mine, and I haven't found an answer to this yet.

I'm trying to relate a person class, to 2 other types of "Person" (SignificantOther and Match), but I'm getting the following error:

Unable to determine the principal end of an association between the types 'Namespace.Models.Person' and 'Namespace.Models.Person'.

Here is my class:

public class Person
{
    public Person()
    {

    }
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Person SignificantOther { get; set; }
    public Person Match { get; set; }
}

I tried using a foreign key data annotation, but that didn't work (maybe I did it wrong). I feel like there is a simple way, that I'm just overlooking. Any ideas?

I'm using code first, and LocalDb.

Tillman32
  • 416
  • 4
  • 9
  • Are you updating the model from the database? Or doing code-first? Also, is it safe to assume you're using SQL Server? – Bardicer Nov 19 '15 at 15:49
  • 1
    I'm quite sure how to do this is discussed in plenty of other questions. Did you try searching on the actual error? Anyway I think the design is flawed. Can a person only have one significant other? I think it's a 0-* relation, where perhaps the application can enforce people having one SO at the same time. The same with matches, if this is what I think it is. – CodeCaster Nov 19 '15 at 15:50
  • @Nick - Good question, I'll update mine. I'm using Code First, and LocalDb (SQLExpress?) – Tillman32 Nov 19 '15 at 15:51
  • do you want to make a self association – lordkain Nov 19 '15 at 15:52
  • @CodeCaster - In the app, I only want the person to have 1 significant other, and 1 match. I can possibly still do an icollection, but that seems overkill. – Tillman32 Nov 19 '15 at 15:52
  • Well then why doesn't http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr answer your question? – CodeCaster Nov 19 '15 at 15:52
  • @CodeCaster - Thats two classes relating each other, I'm only using 1 class. – Tillman32 Nov 19 '15 at 15:53
  • This SO question might help with the overall error: http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr but in general I'd say you need to signify the Person objects with Ids, I.E. add a SignificantOtherId and MatchId and relate that to the SignificantOther.Id and Match.Id. We typically use database diagrams in SQL Server and update the model from the database here. – Bardicer Nov 19 '15 at 15:53
  • @lordkain - I want the association to be a different "person". – Tillman32 Nov 19 '15 at 15:54
  • Then why does your error say _"'Namespace.Models.Match' and 'Namespace.Models.Person'"_? Anyway to make it a 1-1 relation between the same type, you'll have to enforce the relation to be two-way, i.e. person A references person B and vice versa. You can do that the same way. See http://stackoverflow.com/questions/29559187/how-to-define-a-one-to-one-self-reference-using-entity-framework-code-first and so on. – CodeCaster Nov 19 '15 at 15:54
  • can you show us how Match and SignificantOther looks like. they are both different classes / tables? – lordkain Nov 19 '15 at 15:56
  • @CodeCaster - You're right, I copied the error I had from last night, and it was attempting to use a different model. My fault. I'll play around with that answer and see if I can figure it out. Maybe you can provide an example? – Tillman32 Nov 19 '15 at 15:58
  • @lordkain - Same class, same table. – Tillman32 Nov 19 '15 at 15:59
  • 1
    I can't properly look it up at the moment, but I think it's something like `modelBuilder.Entity().HasOptional(p1 => p1.SignificantOther).HasOptional(p2 => p2.SignificantOther)`, and the same for `Match`. – CodeCaster Nov 19 '15 at 16:00
  • @CodeCaster - Awesome, so this does require using the Fluent API. I'll try that out, thanks! – Tillman32 Nov 19 '15 at 16:01

1 Answers1

0

I had to use the Fluent API, and add an optional significant other, as well as match, with optional principals.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().HasOptional(p1 => p1.SignificantOther).WithOptionalPrincipal();
        modelBuilder.Entity<Person>().HasOptional(m => m.Match).WithOptionalPrincipal();
    }

Thanks CodeCaster for pointing me in the right direction!

Tillman32
  • 416
  • 4
  • 9