2

I have two entity models, an Account and User and I am having difficulties implement foreign keys in the dependant model (User). As I am developing an Azure Mobile Service app I need to use the Entity Data interface which provides an 'Id' key field by default.

public class Account : EntityData
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AccountId { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public string EmailAddress { get; set; }
    [Required]
    public string Password { get; set; }
    [Required]
    public string SecurityQuestion { get; set; }
    [Required]
    public string SecurityAnswer { get; set; }
    [Required]
    public bool IsBusiness { get; set; }

    public virtual User User { get; set; }
    public virtual Business Business { get; set; }
}

public class User : EntityData
{
    [Key, Column(Order=1)]
    public virtual string Id { get; set; }
    [Key, Column(Order=2), ForeignKey("Account")]
    public int AccountId { get; set; }
    public int UserId { get; set; }
    [Required]
    public string Forename { get; set; }
    [Required]
    public string Surname { get; set; }

    public virtual Account Account { get; set; }
}

My issue occurs when I specify I want to find 'AccountId' Entity Framework interprets it as 'Account' table, 'Id' column.

Output from Code Migrations:-

User_Account_Source: : Multiplicity is not valid in Role 'User_Account_Source' in relationship 'User_Account'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. User_Account_Target_User_Account_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'AccountId' on entity 'User' does not match the type of property 'Id' on entity 'Account' in the referential constraint 'User_Account'.

Any insight would be highly appreciated!

ocuenca
  • 38,548
  • 11
  • 89
  • 102
davidcrossey
  • 586
  • 3
  • 13
  • Why does `User` have a compound primary key? Shouldn't `Id` be enough? – Gert Arnold Jan 27 '15 at 21:13
  • I don't want to use the GUID Id field generated by EntityData, instead using my own (UserId and AccountId). Trying to make my Key primary but I have to order it due to the EntityData Id key – davidcrossey Jan 27 '15 at 21:30

1 Answers1

1

The reason why EF understands it is one-to-many relationship instead one-to-one is because you are composing your PKs with the Id property, wich is not a FK.In one-to-one relationships one end must be principal and second end must be dependent. Principal end is the one which will be inserted first and which can exist without the dependent one. Dependent end is the one which must be inserted after the principal because it has foreign key to the principal. When configuring one-to-one relationships, Entity Framework requires that the primary key of the dependent also be the foreign key, otherwise EF doesn't see it as one-to-one relation.

public class Account
{
   [Key]
   public int  Id  { get; set; }

   public virtual User User{ get; set; }
}

public class User
{
   [Key, ForeignKey("Account")]
   public int  AccountId { get; set; }

   public virtual Account Account{ get; set; } 
}

If you think about that, it makes sense,otherwise, the below records could happen:

Accounts
Id 
11111111
22222222

Users
Id       AccountId
12rr      11111111
22tt      11111111
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • I can't use `Id` though because that property is already defined in the Base Class `EntityData` - which I need to inherit from for controllers for Web Apis – davidcrossey Jan 27 '15 at 21:44
  • see this http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-use-existing-sql-database/ – saramgsilva Feb 01 '15 at 00:11