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.
Primary /Foreign Key in Entity Framework
Asked Answered
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.
With one-to-many primary key in MyEntity can't be foreign key. There must be either other FK property or no FK property. –
Apropos
@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 ? –
Shuttering
You are the greatest in the world for typing this. Thank you. –
Synchronous
@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! :) –
Janise
@Ladislav: Can you take a look at this question? https://mcmap.net/q/471090/-cascade-delete-rule-in-ef-4-1-code-first-when-using-shared-primary-key-association/114029 –
Janise
I have a question regarding this solution: is there a way to have this same scenario but without having a navigation property <code>Registry</code> on <code>MyEntity</code>, meaning I want a one way navigation only: from principal to dependent only. –
Corgi
Thanks. Really helped out with a difficult mapping on an existing database. –
Latinity
why is the Primary key of MyEntity RegistryId? –
Cheshvan
@Kryan: Because that was part of question. –
Apropos
@LadislavMrnka: where does it say that
RegistryID
's value is bound to Registry
's? –
Gallice 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? –
Clance
@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 in EF but it has its consequences. –
Apropos
@Ladislav: I've been looking for a solution to this problem, and found it right here! Thank you for your explanation! –
Mcclellan
© 2022 - 2024 — McMap. All rights reserved.