I have two models. Town:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace CpuRegistry.Models
{
public enum TownType
{
Город, Посёлок, Село
}
public class Town
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int RegionID { get; set; }
public int? DistrictID { get; set; }
[Required]
public TownType TownType { get; set; }
public virtual ICollection<District> Districts { get; set; }
public virtual ICollection<Primary> Primaries { get; set; }
}
}
and District:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CpuRegistry.Models
{
public class District
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int RegionID { get; set; }
public int? TownID { get; set; }
public virtual ICollection<Town> Towns { get; set; }
public virtual ICollection<Primary> Primaries { get; set; }
}
}
Town can belong to District. Disctrict can belong to Town. Also District can have few towns, Town can have few districts. As you can see, I marked DistrictID and TownID with "?".
When I use command Add-Migration, Visual Studio create migration file, which contains:
CreateTable(
"dbo.TownDistricts",
c => new
{
Town_ID = c.Int(nullable: false),
District_ID = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Town_ID, t.District_ID })
.ForeignKey("dbo.Towns", t => t.Town_ID, cascadeDelete: true)
.ForeignKey("dbo.Districts", t => t.District_ID, cascadeDelete: true)
.Index(t => t.Town_ID)
.Index(t => t.District_ID);
After using command Update-Database, I have next error:
Introducing FOREIGN KEY constraint 'FK_dbo.TownDistricts_dbo.Districts_District_ID' on table 'TownDistricts' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I saw this link: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? Quote from solution:
You must either make the Stage optional in at least one of the entities (i.e. remove the [Required] attribute from the Stage properties)
But I don't have [Required] attribute. Also I tried this solution:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Town>().HasRequired(t => t.Districts).WithMany().WillCascadeOnDelete(false);
modelBuilder.Entity<District>().HasRequired(d => d.Towns).WithMany().WillCascadeOnDelete(false);
}
And have error:
CpuRegistry.DataContexts.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. CpuRegistry.DataContexts.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
And when I open migration file, which creates user tables, I saw something similar to my code:
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
But I cannot find, how developers solved this in models or something else.
Thanks for help!