1

I am trying to create my first app using ASP.NET MVC framework and Entity Framework 6.

I chose to use code first approach and I started by defining my Models.

I have a model called Client with an identity attribute called Id. I have multiple Models that has an attribute called ClientId. The ClientId attribute should have virtual link to the Clients Model.

Here is how my Client model looks like

[Table("clients")]
public class Client
{
    [Key]
    public int id { get; set; }

    public string name { get; set; }
    public string status { get; set; }
    public DateTime created_at { get; set; }
    public DateTime? modified_at { get; set; }

    public Client()
    {
        status = "Active";
        created_at = DateTime.UtcNow;
    }
}

Then here is how I am creating a belong to relation using other models.

[Table("BaseClientsToUsers")]
public class ClientToUser : ModelDefault
{
    [ForeignKey("User")]
    public int UserID { get; set; }

    [ForeignKey("Client")]
    public int ClientId { get; set; }

    [ForeignKey("Team")]
    public int DefaultTeamId { get; set; }

    public DateTime? JoinedAt { get; set; }

    public bool IsActive { get; set; }

    public virtual User User { get; set; }

    public virtual Client Client { get; set; }

    public virtual Team Team { get; set; }

    public ClientToUser()
    {
        DateTime UtcNow = DateTime.UtcNow;

        IsActive = true;

        CreatedAt = UtcNow;

        LastUpdatedAt = UtcNow;
    }



    [Table("BaseTeams")]
    public class Team : ModelDefault
    {
        [MaxLength(250)]
        public string Name { get; set; }

        [ForeignKey("Client")]
        public int ClientId { get; set; }

        public bool IsActive { get; set; }

        public virtual Client Client { get; set; }

        public Team()
        {
            DateTime UtcNow = DateTime.UtcNow;

            IsActive = true;

            CreatedAt = UtcNow;

            LastUpdatedAt = UtcNow;
        }
    }

But, when I try to update my databases I get the following error

Introducing FOREIGN KEY constraint 'FK_dbo.BaseTeams_dbo.BaseClients_ClientId' on table 'BaseTeams' 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 or index. See previous errors.

I am not really sure what could be causing the error but it seems it is because I am creating multiple Foreign keys to the same `Clients model.

How can I fix this error?

Junior
  • 11,602
  • 27
  • 106
  • 212
  • 1
    Did you check [this](http://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths) and [this](http://stackoverflow.com/questions/14489676/entity-framework-how-to-solve-foreign-key-constraint-may-cause-cycles-or-multi)? – Polynomial Proton Jun 05 '16 at 06:19
  • Thank you for the provided resources. I am still not clear how I am creating a circular cascading in the above models. All I am trying to do is create hadOne, BelongsTo, and hasMany relation – Junior Jun 05 '16 at 06:42
  • I tried to disable all the On Delete Cascade but I can't seems to do it right. Here is what I have added to my context class `protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity().HasKey(p => p.Client) .WillCascadeOnDelete(false); }` but I get currely red lined under WillCascadeOnDelete! – Junior Jun 05 '16 at 22:08

1 Answers1

0

Hello @Mike A When I started MVC I got this error too, so you need aditional tables that connects your DB items. So try connect your database items with tables like that: Here is my working example:

[Table("Products")]
public class Product
{
    [Key]
    public string Id { get; set; }
    [Required]
    public string Name { get; set; }
    public string Description { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal InternalPrice { get; set; }
    public string Url { get; set; }
}

[Table("Categories")]
public class Category
{
    [Key]
    public string Id { get; set; }
    [Required]
    public string Name { get; set; }
    public string Url { get; set; }
}

[Table("ProductCategories")]
public class ProductCategory
{
    [Key]
    [Column(Order = 0)]
    public string ProductId { get; set; }
    [Key]
    [Column(Order = 1)]
    public string CategoryId { get; set; }
    public virtual Category Category { get; set; }
}

So you can connect your items without problems hope this will help you.

Archil Labadze
  • 4,049
  • 4
  • 25
  • 42