I am having issues creating a composite key made out of foreign keys in EF 4.3 code first. I would like to solve this problem through data annotations. I am not able to find the right data annotations that says EventID is a foreign Key to Event and is part of the primary key in EventVote table. How do i accomplish this
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
Introducing FOREIGN KEY constraint ‘EventVote_User’ on table ‘EventVotes’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
Try this. You can alternatively put the ForeignKey annotation on the navigation property and reference the name of the FK. Not sure if it will solve your problem but worth a try IMO
Edit: From your error message it sounds like you have an issue similar to what happens with one-to-one relationship mappings. See this article on how to specify using fluent API to disable cascading on one side of each of the relationships.
See this article which addresses this issue. Note they are using Fluent API which could be used instead of the data annotations, to specify your mapping/relationships, or you could use Data Annotations and just use Fluent API to turn off cascading/updates where necessary.
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