Entity Framework Code First 的外键关系(EntityData 问题)
Foreign Key Relationships with Entity Framework Code First (EntityData Issue)
我有两个实体模型,一个帐户和一个用户,我在从属模型(用户)中实现外键时遇到困难。在开发 Azure 移动服务应用程序时,我需要使用默认提供 'Id' 键字段的实体数据接口。
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; }
}
我指定要查找 'AccountId' Entity Framework 时出现问题,将其解释为 'Account' table、'Id' 列。
代码迁移的输出:-
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'.
任何见解将不胜感激!
EF 理解它是一对多关系而不是一对一关系的原因是因为您正在使用 Id
属性 编写 PK,这不是 FK.In 一对一关系一端必须是主体,第二端必须是从属 . 主端是最先插入的,可以不存在从属端。 Dependent end 是必须插入主体之后的那个,因为它有主体的外键。配置一对一关系时,Entity Framework要求依赖的主键也是外键,否则EF不认为是一对一关系
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; }
}
如果你仔细想想,这是有道理的,否则,可能会出现以下记录:
Accounts
Id
11111111
22222222
Users
Id AccountId
12rr 11111111
22tt 11111111
我有两个实体模型,一个帐户和一个用户,我在从属模型(用户)中实现外键时遇到困难。在开发 Azure 移动服务应用程序时,我需要使用默认提供 'Id' 键字段的实体数据接口。
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; }
}
我指定要查找 'AccountId' Entity Framework 时出现问题,将其解释为 'Account' table、'Id' 列。
代码迁移的输出:-
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'.
任何见解将不胜感激!
EF 理解它是一对多关系而不是一对一关系的原因是因为您正在使用 Id
属性 编写 PK,这不是 FK.In 一对一关系一端必须是主体,第二端必须是从属 . 主端是最先插入的,可以不存在从属端。 Dependent end 是必须插入主体之后的那个,因为它有主体的外键。配置一对一关系时,Entity Framework要求依赖的主键也是外键,否则EF不认为是一对一关系
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; }
}
如果你仔细想想,这是有道理的,否则,可能会出现以下记录:
Accounts
Id
11111111
22222222
Users
Id AccountId
12rr 11111111
22tt 11111111