original.roland
original.roland

Reputation: 700

C# Entity Framework - beginner

The database relation looks like this at the moment: https://i.sstatic.net/uzvnp.png

I've got a connector table called friendship, which holds 2 values and a key ID. This table describes that X friends Y, but Y might not friend X. So it's some kind of linear stuff.

I would like to model the same in Entity Framework, but I fail all the time because I get this error:

may cause cycles or multiple cascade paths.

I made two tables in EF:

class Friendship
{
    [Key]
    public int id { get; set; }
    public int whoid { get; set; }
    public int whomid { get; set; }

    [ForeignKey("whoid")]
    public virtual Person who { get; set; }

    [ForeignKey("whomid")]
    public virtual Person whom { get; set; }
}

class Person
{
    [Key]
    public int id { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string name { get; set;} 
    public string city { get; set; }
    public string street { get; set; }
    public string hnum { get; set; }
    public string bday { get; set; }

    [InverseProperty("who")]
    public virtual List<Friendship> wholist { get; set; }

    [InverseProperty("whom")]
    public virtual List<Friendship> whomlist { get; set; }
}

Upvotes: 3

Views: 99

Answers (2)

Dharmesh
Dharmesh

Reputation: 105

I think you need to write your code as below and add correct relationship.

class Friendship
{
    [Key]
    public int id { get; set; }

    [ForeignKey("who")]
    public int whoid { get; set; }

    [ForeignKey("whom")]
    public int whomid { get; set; }


    public virtual Person who { get; set; }


    public virtual Person whom { get; set; }
}

class Person
{
    [Key]
    public int id { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string name { get; set;} 
    public string city { get; set; }
    public string street { get; set; }
    public string hnum { get; set; }
    public string bday { get; set; }

    [InverseProperty("who")]
    public virtual List<Friendship> wholist { get; set; }

    [InverseProperty("whom")]
    public virtual List<Friendship> whomlist { get; set; }
}

Also you need to add below code for relationship between entities in your DB context file.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Friendship>()
                    .HasRequired(e => e.who)
                    .WithMany(t => t.wholist)
                    .HasForeignKey(e => e.whoid)
                    .WillCascadeOnDelete(false);

        modelBuilder.Entity<Friendship>()
                    .HasRequired(e => e.whom)
                    .WithMany(t => t.whomlist)
                    .HasForeignKey(e => e.whomid)
                    .WillCascadeOnDelete(false);   




}

Upvotes: 2

Saneesh kunjunni
Saneesh kunjunni

Reputation: 548

In Entityframework u cannot add more than one foreign key of same Class that whey it getting Error.. remove any of this....

[ForeignKey("whoid")]
public virtual Person who { get; set; }

[ForeignKey("whomid")]
public virtual Person whom { get; set; }

I think this problem you also get when adding in sqlserver

Upvotes: 0

Related Questions