有3个实体:
用户有 <!doctype html>
<html lang="en">
<head> <meta charset="utf-8">
<title>jQuery UI Dialog - Default functionality</title>
<link rel="stylesheet" href="jquery-ui.css">
<script src="jquery-1.10.2.js"></script>
<script src="jquery-ui.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="style.css">
<script type="text/javascript">
$(function() {
$('#clickMe').click(function(event) {
var link = this;
$('<div>').dialog({
modal: true,
open: function ()
{
$(this).load($(link).attr("href"));
},
height: 400,
width: 400,
title: 'This is popup window',
show: 'puff',
hide: 'puff'
});
});
});
</script>
</head>
<body>
<button id="clickMe" href="http://www.google.com/">Google</button>
</body>
</html>
和ICollection<Event>
。
事件具有虚拟用户导航属性,ICollection<Review>
审核类
的结构ICollection<Review>
我得到了 - 引入FOREIGN KEY可能导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。无法创建约束。查看以前的错误。
我可以通过 public int Id { get; set; }
public string Text { get; set; }
public int Mark { get; set; }
public ReviewStatus ReviewStatus { get; set; }
public int EventId { get; set; }
public virtual Event Event { get; set; }
public int UserId { get; set; }
public virtual AppUser User { get; set; }
的{{1}}方法添加此内容来解决此问题:
ModelCreating
但在此之后我无法从数据库中删除事件或用户,因为发生了外键错误。那还有另一种解决方案吗?
答案 0 :(得分:1)
您应该在删除时决定在外键的位置无法删除或插入null,您可以删除User
并为任何已删除的Event
插入null Review
中的外键并且有孤立Review
,以下是使Event
与Review
关系成为可空的代码:
modelBuilder.Entity<Review>()
.HasOptional(t => t.Event)
.WithMany(t => t.Reviews)
.HasForeignKey(d => d.EventId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Review>()
.HasOptional(p => p.Event)
.WithMany(p => p.Reviews)
.HasForeignKey(p => p.EventId);
还要记得在删除用户时将Event
和Review
延迟加载Include
,否则null
将无法插入,其他要点就是记住当你如果User
有很多Events
且Events
有很多Reviews
您需要为Event
Review
进行var eventItems = _db.Events.Include(p => p.Reviews).(s => s.UserIdfk == UserId);
foreach (var item in eventItems)
{
_db.Events.Remove(item);
}
空插,EF并没有为你做这件事,比如打击例子:
public SelectList GetRoutingActionList()
{
Dictionary<string, string> routingActions = new Dictionary<string, string> {
{ "Approved", "Approve" },
{ "Return To", "Return To .." },
{ "Return To Sender", "Return To Sender" },
{ "Disapprove", "Disapprove" },
{ "Set On Hold", "Set On Hold" }
};
SelectList routingActionList = new SelectList(routingActions, "Key", "Value");
return routingActionList.Where(x => x.Key != "Disapprove")
.ToList();
}
我希望我能够传达一般方法。