我有这个对象:
public class Profile
{
public int Id {get;set;}
public List<User> Users {get;set;}
}
我有一个删除个人资料的页面..所以如果没有相关用户,我想删除个人资料..
我试过这个:
var profile = _db.Profile
.Include(p => p.Users)
.SingleOrDefault(p => p.Id == id);
_db.Profile.Remove(profile);
_db.SaveChanges();
但这会自动删除所有相关的用户(我不明白为什么,因为我没有为此设置任何特殊配置)。
我怎样才能避免这种情况?当我尝试 SaveChanges 时如何获得异常?
我很想避免Users.Count
直接在我的代码中检查来做到这一点。