我想在ASP.NET Core中创建用户关系表并遇到一些问题。如果由于这个原因我必须禁用级联删除,我该如何防止孤儿?
错误:
private void Refresh_Click(object sender, RoutedEventArgs e)
{
string url = @"https://api.twitch.tv/kraken/streams/camoduck?client_id=xskte44y2wfqin464ayecyc09nikcj";
var json = new WebClient().DownloadString(url);
Rootobject r = JsonConvert.DeserializeObject<Rootobject>(json);
Console.WriteLine(r.stream);
if r.stream.game = "Grand Theft Auto V"
{
_1GUnit1.Background = Brushes.Red;
}
}
ApplicationDbContext.cs:
Introducing FOREIGN KEY constraint 'FK_UserRelationships_AspNetUsers_User2Id' on table 'UserRelationships' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
我目前的模特:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<UserRelationship>().HasKey(x => new { x.User1Id, x.User2Id });
}
public DbSet<UserRelationship> UserRelationships { get; set; }
}
答案 0 :(得分:0)
您可以根据需要使用ApplicationUsers,因此EF会默认将删除两次级联到同一个类(请参阅here)。
你需要告诉它不要:
builder.Entity<UserRelationship>().HasOne(ur => ur.User1).WithMany().HasForeignKey(ur => ur.UserId1).WillCascadeOnDelete(false);
builder.Entity<UserRelationship>().HasOne(ur => ur.User2).WithMany().HasForeignKey(ur => ur.UserId2).WillCascadeOnDelete(false);