0

我需要删除一个实体的记录,但保留与之相关的另一个实体的所有记录:

要删除的实体记录是:

public class Ask
{
    // Primary properties
    public int Id { get; set; }
    public string Title { get; set; }
    public string Message { get; set; }
    public DateTime DateCreated { get; set; }
}

删除 Ask 记录后我要保留的相关记录是 MessageAsk 类型:

public class Message
{
    // Primary properties
    public int Id { get; set; }
    public string NameFrom { get; set; }
    public string EmailFrom { get; set; }
    public string TelephoneFrom { get; set; }
    public string Title { get; set; }
    public string MessageText { get; set; }
    public bool? Approved { get; set; }
    public DateTime? DateCreated { get; set; }
    public DateTime? DateRead { get; set; }

    // Navigation properties
    public Member MemberFrom { get; set; }
    public Member MemberTo { get; set; }
    public MessageType MessageType { get; set; }
    public Message MessageParent { get; set; }
}

public class MessageAsk : Message
{
    public Ask Ask { get; set; }
}

恢复,我想删除一个 Ask 并保留所有相关的 MessageAsk。

编辑:我使用服务删除:

    private readonly IRepository<Ask> _askRepository;
    private readonly IRepository<MessageAsk> _messageAskRepository;

    public bool Delete(int askId)
    {
        try
        {
            Ask askToDelete = _askRepository.GetById(askId);

            IList<MessageAsk> relatedMessageAsks = _messageAskRepository.Query.Where(m => m.Ask.Id == askId).ToList();

            _askRepository.Delete(askToDelete);

            _askRepository.Save();

         }
        catch 
        { 
            return false; 
        }

        return true;
    }

我使用存储库来删除实体:

public class Repository<T> : IRepository<T> where T : class
{
    protected DbContext _dataContext;
    protected DbSet<T> _dbSet;

    public Repository(DbContext context)
    {
        _dataContext = context;
        _dbSet = _dataContext.Set<T>();
    }

    public T NewEntityInstance()
    {
        return _dbSet.Create();
    }

    public void Delete(T entity)
    {
        if (_dataContext.Entry(entity).State == EntityState.Detached)
        {
            _dbSet.Attach(entity);
        }
        _dbSet.Remove(entity);
    }

   public virtual void Delete(object id)
    {
        T entity = _dbSet.Find(id);

        Delete(entity);
    }

    public T GetById(int id)
    {
        return _dbSet.Find(id);
    }

    public virtual IQueryable<T> Query
    {
        get
        {
            return _dbSet.AsNoTracking(); <------ SOURCE OF THE PROBLEM - I HAD TO REMOVE THE ASNOTRACKING OPTION TO SOLVE THE PROBLEM
        }
    }

}

我现在得到的错误:

"The DELETE statement conflicted with the REFERENCE constraint "FK_Messages_Asks". The conflict occurred in database "Heelp", table "dbo.Messages", column 'Ask_Id'.

谢谢

4

1 回答 1

1

如果您的关系是可选的(即MessageAsk表到Ask表的外键允许NULL值),您可以这样做:

using (var context = new MyContext())
{
    var askToDelete = context.Asks.Single(a => a.Id == askToDeleteId);
    var relatedMessageAsks = context.MessageAsks
        .Where(m => m.Ask.Id == askToDeleteId)
        .ToList();
    // or just: context.MessageAsks.Where(m => m.Ask.Id == askToDeleteId).Load();

    context.Asks.Remove(askToDelete);
    // or DeleteObject if you use ObjectContext

    context.SaveChanges();
}

(或者context.Messages.OfType<MessageAsk>()...context.MessageAsks...如果您在上下文中没有派生类型的集合,而不是)

您无需在此处显式设置该MessageAsk.Ask属性。nullEF 将在askToDelete删除时自动执行此操作,并在数据库中MessageAsk使用 FK =更新。NULL

如果需要关系(不允许 FK 的 s),则它不起作用,因为当主体 ( ) 将被删除NULL时,您将违反数据库中的引用外键约束。在这种情况下askToDelete,您需要在删除.relatedMessageAsksAskaskToDelete

于 2013-03-11T19:23:15.507 回答