E
E
eRKa2016-07-26 14:45:01
ASP.NET
eRKa, 2016-07-26 14:45:01

How to set up one entity in EF with two external references to another entity?

I use code-first.
There is one essence

public class Stage
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
        
        public virtual ICollection<Motion> Motions { get; set; }

        public Stage()
        {
            Motions = new List<Motion>();
        }
    }

There is a second entity
public class Motion
    {
        public int Id { get; set; }
        public string Description { get; set; }

        public int NextStageId { get; set; }
        public virtual Stage NextStage { get; set; }

        public int OwnerStageId { get; set; }
        public virtual Stage OwnerStage { get; set; }
    }

In essence, Stage is like a linked list node, and Motion is links to these nodes. It turns out that Stage knows about other nodes only through these links. The link (Motion) must know who owns (OwnerStage) and refer to other Stage (NextStage) , Stage should not have feedback through NextStage.
There is a setting
public MotionMapper()
        {
            this.ToTable("Motion");
            this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(x => x.Id).IsRequired();
            this.Property(x => x.Description).IsRequired();

            this.HasRequired(x => x.OwnerStage)
                .WithMany(x => x.Motions)
                .HasForeignKey(x => x.OwnerStageId);

            this.HasOptional(x => x.NextStage)
                .WithMany();  // Вот тут я теряюсь что писать, перепробовал много вариантов)
        }

The base does not want to be generated. I comment on NextStage in Motion - everything is fine. So it's in this field.
Please help me figure out how to set this up.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
atticus_finch, 2016-07-26
@kttotto

stackoverflow.com/questions/9437366/entity-framewo...
I understand that you have a similar situation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question