First combo code key made from foreign keys ef 4.3

I am having problems creating a composite key made from foreign keys in EF 4.3 code. I would like to solve this problem with data annotations. I cannot find the correct data annotations that say the EventID is the foreign key to the event and is part of the primary key in the EventVote table. How to do it?

   public class EventVote
{

    [Key, Column(Order = 0)]
    [ForeignKey("Event")]
    public int EventID { get; set; }

    [Key, Column(Order = 1)]
    [ForeignKey("User")]
    public int UserID { get; set; }

    [Required]
    public DateTime VoteTime { get; set; }

    [Required]
    public bool Vote { get; set; }



    public virtual Event Event { get; set; }
    public virtual User User { get; set; }
}

      

error Representation of FOREIGN KEY constraint "EventVote_User" in table "EventVotes" may cause loops or multiple cascading paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or change other FOREIGN KEY constraints. Failed to create constraint. See previous errors.

+3


source to share


1 answer


Try it. You can also put ForeignKey annotation in navigation property and give FK name. Not sure if it will solve your problem, but worth trying IMO

public class EventVote
{

    [Key, Column(Order = 0)]
    public int EventID { get; set; }

    [Key, Column(Order = 1)]
    public int UserID { get; set; }

    [Required]
    public DateTime VoteTime { get; set; }

    [Required]
    public bool Vote { get; set; }


    [ForeignKey("EventID")]
    public virtual Event Event { get; set; }
    [ForeignKey("UserID")]
    public virtual User User { get; set; }
}

      

Edit: From your error message, it sounds like you have a similar problem to what happens with one-to-one relationship mappings. See this article for how to specify using the fluent API to disable cascading on one side of each relationship.



See this article for a discussion of this issue. Note that they use the Fluent API, which could be used instead of data annotation, specify your mappings / relationships, or you can use data annotations and just use the Fluent API to disable cascading / updates where needed.

http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations. aspx

+3


source







All Articles