我重新实现了由 SimpleMembershipProvider 自动创建的数据库。实际上我有一个关于 2 个表链接的问题:
create table user_profiles
(
id int not null identity, /* PK */
username varchar(128) not null,
.........
);
create table membership
(
userid int not null, /* FK to user_profile. */
..............
);
我想在初始 POCO 类之间创建关系:
public class UserProfile : BaseType
{
public virtual Membership Membership { get; set; }
......
public string UserName { get; set; }
......
}
public class Membership
{
public virtual int UserId { get; set; }
public virtual UserProfile User { get; set; }
......
}
在用作 PK 的Membership
属性中,与数据库中的 FK 同时使用。UserId
我尝试了以下配置:
public class UserProfileConfiguration : EntityTypeConfiguration<UserProfile> {
public UserProfileConfiguration() {
HasKey(k => k.Id);
Map(m => m.ToTable("user_profiles"));
HasRequired(t => t.Membership)
.WithRequiredPrincipal(t1 => t1.User)
.Map(m => m.MapKey("userid"));
....
}
}
public class MembershipConfiguration : EntityTypeConfiguration<Membership> {
public MembershipConfiguration() {
HasKey(k => k.UserId);
Map(m => m.ToTable("webpages_Membership"));
//Property(x => x.UserId).HasColumnName("userid");
}
}
当MembershipConfiguration
注释掉的行(如示例中)命令Add-Migration
在迁移命令中创建 2 条记录:
c => new {
UserId = c.Int(nullable: false, identity: true),
.............
userid = c.Int(nullable: false),
如果我取消注释命令失败并显示错误消息Each property name in a type must be unique. Property name 'userid' was already defined.
我如何声明所需的结果,同时使用列“userid”作为 PK 和 FK?