Sandeep Shekhawat
Sandeep Shekhawat

Reputation: 695

One to one relationship in Entity Framework 5.0 Code First Approach

I want to develop a relationship between two entities. Both entities has one to one relationship and both has navigation property for each other means I can access entity then access another associated entity with navigation property. Let’s see an example.

Suppose I have two entity like User and UserProfile. Each user has only one profile and one user profile has only one User. I can access User Profile from user like User.UserProfile and also access User from UserProfile like UserProfile.User. I want to implement above scenario in my entities and did not get solution. Please suggest your thought how I can achieve this. My way is following to implement one to one relationship in entity framework code first.

1. Create a Base entity

using System;        
namespace Ioc.Core
{
  public abstract  class BaseEntity<T> where T: struct
    {
      public T ID { get; set; }
      public DateTime AddedDate { get; set; }
      public DateTime ModifiedDate { get; set; }
      public string IP { get; set; }
    }
}

2. Create User Entity

using System;    
namespace Ioc.Core.Data
{
   public class User : BaseEntity<Guid>
    {
       public string UserName { get; set; }
       public string Email { get; set; }
       public string Password { get; set; }      
    }
}

3. Create User Profile Entity

using System;
namespace Ioc.Core.Data
{
  public class UserProfile : BaseEntity<int>
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Address { get; set; }
      public Guid UserId { get; set; }
      public virtual User User { get; set; }
    }
}

4. Mapping User to create table

using System.Data.Entity.ModelConfiguration;
using Ioc.Core.Data;
namespace Ioc.Data.Mapping
{
   public class UserMap :EntityTypeConfiguration<User> 
    {
       public UserMap()
       {
           //key
           HasKey(t => t.ID);
           //properties
           Property(t => t.UserName).IsRequired();
           Property(t => t.Email).IsRequired();
           Property(t => t.Password).IsRequired();
           Property(t => t.AddedDate).IsRequired();
           Property(t => t.ModifiedDate).IsRequired();
           Property(t => t.IP);
           //table
           ToTable("Users");
       }
    }
}

5. Mapping UserProfiles to create table

using System.Data.Entity.ModelConfiguration;
using Ioc.Core.Data;
namespace Ioc.Data.Mapping
{
   public class UserProfileMap : EntityTypeConfiguration<UserProfile>
    {
       public UserProfileMap()
       {
           //key
           HasKey(t => t.ID);
           //properties           
           Property(t => t.FirstName).IsRequired().HasMaxLength(100).HasColumnType("nvarchar");
           Property(t => t.LastName).HasMaxLength(100).HasColumnType("nvarchar");
           Property(t => t.Address).HasColumnType("nvarchar");
           Property(t => t.AddedDate).IsRequired();
           Property(t => t.ModifiedDate).IsRequired();
           Property(t => t.IP);
           //table
           ToTable("UserProfiles");
           //relation
           HasRequired(t => t.User).WithMany().HasForeignKey(t => t.UserId).WillCascadeOnDelete(false);
       }       
    }
}

I am thinking that I don’t need define the primary key ID in UserProfile entity and the UserId property will be also primary key and foeign key for UserProfile table but how can define navigation property in User Entity for User Profile?

Upvotes: 0

Views: 288

Answers (2)

Rowan Miller
Rowan Miller

Reputation: 2090

Here is the best way to model this. I trimmed out base classes and other unrelated configuration just to get the core of how to configure a required:required relationship. As you mentioned in your question, the primary of UserProfile also doubles as the foreign key to User.

public class User
{
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }

    public UserProfile UserProfile { get; set; }
}

public class UserProfile
{
    public Guid UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }

    public virtual User User { get; set; }
}

public class UserProfileMap : EntityTypeConfiguration<UserProfile>
{
    public UserProfileMap()
    {
        HasKey(p => p.UserId);

        HasRequired(p => p.User).WithRequiredDependent(u => u.UserProfile);
    }
}

Upvotes: 1

Anshul Nigam
Anshul Nigam

Reputation: 1628

I am not sure how you can do it with fluent api but with model you can do

public class User : BaseEntity<Guid>
    {
       public string UserName { get; set; }
       public string Email { get; set; }
       public string Password { get; set; }      
       public virtual UserProfile userProfile { get; set;}
    }
}
3. Create User Profile Entity

using System;
namespace Ioc.Core.Data
{
  public class UserProfile : BaseEntity<int>
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Address { get; set; }
      public Guid UserId { get; set; }
      public virtual User User { get; set; }
    }
}

NOTE: If this had helped you dont forgot to mark it as answer and vote it up :)

Upvotes: 0

Related Questions