0

I reimplementing database created automatically by SimpleMembershipProvider. Actually I have a question about 2 tables linking:

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. */
  ..............
);

I'd like to create relationship between initial POCO classes:

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; }
    ......
}

In Membership property UserId used as PK and in the same time as FK in database. I tried following configurations:

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");
    }
}

When line in MembershipConfiguration commented out (like in sample) command Add-Migration creates 2 records in migration command:

c => new {
  UserId = c.Int(nullable: false, identity: true),
  .............
  userid = c.Int(nullable: false),

If I uncommenting it command failed with error message Each property name in a type must be unique. Property name 'userid' was already defined.

How could I claim required result, use column 'userid' as PK and FK in the same time?

Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
  • 1
    You'd probably not expect it, but the answer is [here](http://stackoverflow.com/a/10108574/861716). EF has no support for one-to-one foreign key associations (i.e. associations with object reference + primitive FK property). This is visible in the fact that there is no `HasForeignKey` method after `WithRequiredPrincipal`. – Gert Arnold Apr 07 '13 at 11:52
  • Ok, I agree to add another FK or remove all FK and use only nabigation properties with remapping to correct column names. But I do not understand how. – Alex G.P. Apr 07 '13 at 12:40

0 Answers0