31

I have created an entity class in my MVC 3 application. One of the attribute named RegistryId is primary key as well as foreign key. How can I make a column primary key as well as Foreign key ? I am not using EF ORM designer. I am coding classes by hand.

Mathieu
  • 4,449
  • 7
  • 41
  • 60
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316

1 Answers1

55

I think by "not using EF ORM designer" you mean new DbContext API from EF 4.1. Because if you don't mean DbContext API you still have to use EDMX (designer).

You can either use data annotations (System.ComponentModel.DataAnnotations): KeyAttribute and ForeignKeyAttribute:

public class Registry
{
    public virtual int Id { get; set; }
    public virtual MyEntity MyEntity { get; set; }
}

public class MyEntity
{
    [Key, ForeignKey("Registry")]
    public virtual int RegistryId { get; set; }

    public virtual Registry Registry { get; set; }
}

Or you can use fluent API (overriding OnModelCreating in your derived context):

(Edit: fluent mapping was reversed and incomplete)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<MyEntity>()
                .HasKey(e => e.RegistryId);
    modelBuilder.Entity<MyEntity>()
                .Property(e => e.RegistryId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    modelBuilder.Entity<MyEntity>()
                .HasRequired(e => e.Registry)
                .WithRequiredDependent(r => r.MyEntity);
}

Where MyEntity is your entity with FK and Registry is principal entity in 1:1 relationship.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 1
    With one-to-many primary key in MyEntity can't be foreign key. There must be either other FK property or no FK property. – Ladislav Mrnka Mar 22 '11 at 16:44
  • @Ladislav: When I try to use [ForeignKEy("")], It asks for column name. do I need to give Registry inside it or Registry entity's Id ? – DotnetSparrow Mar 22 '11 at 16:48
  • You are the greatest in the world for typing this. Thank you. – ElvisLives Apr 27 '11 at 18:39
  • 1
    @Ladislav: You're the man! :D I've read a lot of material on the internet but none of them helped. This is a simple and effective answer. God bless your life! :) – Leniel Maccaferri Apr 27 '11 at 21:17
  • @Ladislav: Can you take a look at this question? http://stackoverflow.com/q/5825799/114029 – Leniel Maccaferri Apr 28 '11 at 22:33
  • I have a question regarding this solution: is there a way to have this same scenario but without having a navigation property Registry on MyEntity, meaning I want a one way navigation only: from principal to dependent only. –  Sep 21 '11 at 13:15
  • Thanks. Really helped out with a difficult mapping on an existing database. – NickH Sep 27 '12 at 16:25
  • why is the Primary key of MyEntity RegistryId? – Karan Oct 02 '12 at 16:30
  • @Kryan: Because that was part of question. – Ladislav Mrnka Oct 02 '12 at 17:10
  • @LadislavMrnka: where does it say that `RegistryID`'s value is bound to `Registry`'s? – TDaver Oct 03 '12 at 12:25
  • This is a very good answer, but, is there any solution that does not imply that I have to change my model to add foreign key properties to objects that hold navigation properties? – Isaac Llopis Dec 18 '12 at 13:51
  • @IsaacLlopis: This answer targets one-to-one relation where foreign key is also primary key (you cannot have entity without primary key). If you model one-to-many relation you don't have to add foreign key properties to your dependent entities - that is a difference between [independent and foreign key association](http://stackoverflow.com/questions/5281974/code-first-independent-associations-vs-foreign-key-associations/5282275#5282275) in EF but it has its consequences. – Ladislav Mrnka Dec 19 '12 at 10:44
  • @Ladislav: I've been looking for a solution to this problem, and found it right here! Thank you for your explanation! – peter70 Jun 04 '14 at 13:10