0

I want to create one to one relationship in EF with MVC but getting little bit confused

here are my model...

public class Event
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public Guid CategoryId { get; set; }
    public string EventTitle { get; set; }
    public string ImgUrl { get; set; }
    public string Venue { get; set; }
    public int SeatsAvailable { get; set; }
    public string Description { get; set; }
    public DateTime EventDate { get; set; }
    public DateTime EventAddedDate { get; set; }

    [Required]
    [ForeignKey("CategoryId")]
    public virtual Category Category { get; set; }

}

And

public class Category
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string CategoryName { get; set; }
    public virtual Event Event { get; set; }
}

I want to store the Category Id in Event so I created relation in fluentApi like this...

modelBuilder.Entity<Event>()
            .HasRequired(a => a.Category)
            .WithOptional(b => b.Event);

but getting this error when updating database...

System.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Event_Category_Source' in relationship 'Event_Category'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
Abhay
  • 47
  • 1
  • 10
  • Perhaps this helps: http://stackoverflow.com/questions/35639341/configuring-many-one-to-one-relationships-with-entity-framework-code-first-and-f?rq=1 – Jose Luis Apr 20 '16 at 19:01

2 Answers2

0

See the answer: https://stackoverflow.com/a/18726038/4254779

modelBuilder.Entity<Event>()
        .HasKey(t => t.Id);

modelBuilder.Entity<Event>()
    .HasRequired(a => a.Category)
    .WithOptional(b => b.Event);
Community
  • 1
  • 1
0

EF is not going to let you create a one to one relationship between two entities with independent PKs and mapping a FK property at the same time.This is because of Entity Framework’s requirement that the primary key of the dependent entity should be used as the foreign key too.

public class Principal
{
  [Key]
  public Guid Id{get;set;}

  public virtual Dependent Dependent{get;set}
}

public class Dependent
{
  [Key,ForeignKey("Principal" )]
  public Guid PrincipalId{get;set;}

  public virtual Principal Principal{get;set}
}

To resolve this issue, remove the FK property from Event and remove the ForeignKey attribute from Category navigation property. The Fluent Api configuration is not needed in this case.

public class Event
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    //public Guid CategoryId { get; set; }

    [Required]
    //[ForeignKey("CategoryId")]
    public virtual Category Category { get; set; }

}
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • So how would I store the categoryId when I'll create Event of particular category. – Abhay Apr 21 '16 at 06:27
  • EF will do for you. If you check your DB you will see you still have a Fk, but EF is who is going to manage that Fk in case you set the Category navigation property – ocuenca Apr 21 '16 at 11:22