【发布时间】:2015-03-26 12:39:40
【问题描述】:
我有两个实体模型,一个帐户和一个用户,我在依赖模型(用户)中实现外键时遇到了困难。在开发 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”实体框架将其解释为“Account”表、“Id”列时,会出现我的问题。
代码迁移的输出:-
User_Account_Source: : 多重性在角色中无效 关系“User_Account”中的“User_Account_Source”。因为 Dependent Role 属性不是关键属性,上限 从属角色的多重性必须是“*”。 User_Account_Target_User_Account_Source: : 所有属性的类型 在引用约束的 Dependent Role 中必须与 Principal Role 中的相应属性类型。的类型 实体“用户”上的属性“帐户 ID”与 引用约束中实体“帐户”的属性“Id” 'User_Account'。
任何见解都将受到高度赞赏!
【问题讨论】:
-
为什么
User有一个复合主键?Id还不够吗? -
我不想使用 EntityData 生成的 GUID Id 字段,而是使用我自己的(UserId 和 AccountId)。尝试将我的 Key 设为主键,但由于 EntityData Id 键,我必须订购它
标签: entity-framework azure ef-code-first foreign-keys ado.net-entity-data-model