There are 2 tables:

public class Station : IEntitie { [Key] public int Id { get; set; } public string Description { get; set; } } public class OperativeSchedule : IEntitie { [Key] public int Id { get; set; } public virtual Station DispatchStation { get; set; } //для таблицы station надо задать ключ public virtual Station StationOfDestination { get; set; } //для таблицы station надо задать ключ } 

It is necessary to set the key in the Station table to communicate with OperativeSchedule . Now when deleting a Station from Stations an exception is thrown if DispatchStation or StationOfDestination refers to this station.

  • Maybe this will help you entityframeworktutorial.net/code-first/… - tCode
  • I read it, but in the description there is no example with multiple links. If you know how to set names for DispatchStation and StationOfDestination in the Station table, please write. - Aldmi
  • unfortunately he is still not strong in Entity) - tCode
  • entityframeworktutorial.net/code-first/… is more suitable. But there is also an example of 1 to many. I have 1 to 1. - Aldmi
  • Is the declaration of relationships through attributes mandatory? Fluent gives much more room for action ... - kimaman2

1 answer 1

Here is a brief and good description for one-to-many and many-to-many: http://andrey.moveax.ru/post/mvc3-in-depth-entity-framework-03-code-first- conventions

Only I have the impression that the question is not about you, but about the cascade deletion and then you need to look towards the fluent API ( one , two ):

 modelBuilder.Entity<Product>() .HasOptional(p => p.Category) .WithMany() .WillCascadeOnDelete(true); 

Let us formulate the question more precisely. For example, I’m not sure that you need a many-to-many connection. What is the logic of work?