0

I have those two entities:

public class User
{     
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public string IdsrvUniqueId { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    public string About { get; set; }
    public GenderEnum Gender { get; set; }

    public DateTime BirthDate { get; set; }
    public string PhoneNumber { get; set; }
}


public class NotificationSetting
{       
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public bool AutomaticallySubscribeToAllGroups { get; set; }

    public bool AutomaticallySubscribeToAllGroupsWithTag { get; set; }
}

what I want to do is implemente 1:1 relationship between them but currently with no success. I tried to use data notations and decorate them to look like this:

public class User
    {     
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        ..........

        // Navigation properties
        public virtual NotificationSetting NotificationSettings { get; set; }
    }

public class NotificationSetting
    {       
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [ForeignKey("User")]
        public Guid Id { get; set; }

        ....

        // Navigation properties
        public virtual User User { get; set; }
    }

It compiles, generates migration file but when I try to add new user I get an error. Can you please tell me what is wrong here and How can I achieve my goal.

user2128702
  • 2,059
  • 2
  • 29
  • 74

2 Answers2

0

I think the ForeignKey attribute is on the wrong property.

public class User
{     
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    ..........

    // Navigation properties
    public virtual NotificationSetting NotificationSettings { get; set; }
}

public class NotificationSetting
{       
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    ....
    [ForeignKey("User")]
    public Guid UserId { get; set; }
    // Navigation properties           
    public virtual User User { get; set; }
}

That is how I always define them. The way you posted it, it uses the PK of NotificationSetting as the FK for User

Mats391
  • 1,199
  • 7
  • 12
  • What goes in the User entity? Aren't there any Data notations there? – user2128702 Nov 09 '16 at 12:59
  • User stays same as you had it. Updated my answer with both User and NotificationSetting – Mats391 Nov 09 '16 at 13:17
  • When try to generate my migration I get this error: NotificationSetting_User_Source: : Multiplicity is not valid in Role 'NotificationSetting_User_Source' in relationship 'NotificationSetting_User'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. – user2128702 Nov 09 '16 at 13:20
  • I think this will help http://stackoverflow.com/questions/26389707/ef-code-first-one-to-one-relationship-multiplicity-is-not-valid-in-role-in-re – Mats391 Nov 09 '16 at 13:31
  • Unfortunately non of these works. I still don't know what's the reason for getting this error: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'. – user2128702 Nov 09 '16 at 14:11
0

When you configure entities to have one-to-one relationship, the key in your dependent entity(NotificationSetting) is foreign-key of principal entity(User) too, so it could not be auto generated field in database, so the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] in NotificationSetting should be remove:

public class User
{     
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    ..........

    // Navigation properties
    public virtual NotificationSetting NotificationSettings { get; set; }
}

public class NotificationSetting
{       
    [Key]
    [ForeignKey("User")]
    public Guid Id { get; set; }

    ....

    // Navigation properties
    public virtual User User { get; set; }
}
Masoud
  • 8,020
  • 12
  • 62
  • 123