我已经阅读了所有可以找到的内容,但仍然无法解决我的问题。我正在使用PostgreSQL,并且有一个抽象类和一个子类。我希望当父项被删除后,所有相关的依赖项也将被删除。
这是我的班级样子:
public abstract class NotificationModel
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid id { get; set; }
public string type { get; set; }
// and other common fileds
public abstract NotificationModel buildObject(JObject json);
}
我的孩子班级:
class SmsNotificationModel : NotificationModel
{
public override NotificationModel buildObject(JObject json){return dummyObj;}
public SmsBody Body { get; set; }
public Guid Fk_sms { get; set; }
}
和SmsBody
class SmsBody
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public List<string> Recipients { get; set; }
public string Route { get; set; }
//edit
public SmsNotificationModel smsNotModel { get; set; }
}
在DbContext中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SmsNotificationModel>()
.HasOne(em => em.Body)
.WithOne(d => d.smsNotModel)
.HasForeignKey<SmsNotificationModel>(f => f.Fk_sms)
.OnDelete(DeleteBehavior.Cascade);
base.OnModelCreating(modelBuilder);
}
我不明白我在做什么错。该表被标记为级联,但是当我尝试删除此表时,它根本不起作用。有人可以给我一些有关如何设置它的提示。
====稍后编辑
我有一个要删除的通用存储库(模板参数是NotificationModel抽象类)
public class GenericRepository<T> : IGenericRepository<T> where T :class
{
private readonly ApplicationDbContext _context;
private DbSet<T> table = null;
public GenericRepository(ApplicationDbContext _context) {
this._context = _context;
table = _context.Set<T>();
}
public void Delete(object id){
T existing = table.Find(id);
table.Remove(existing);
}
}
一对一关系配置应用于发布方案。实体定义。
SmsNotification
public class SmsNotificationModel : NotificationModel
{
public SmsBody Body { get; set; }
// ...
}
SmsBody
public class SmsBody
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
// ...
// I moved the Id relation here.
// To me, it seems like Notification is the one-level-up absatraction model.
// This may be changed at any time.
public SmsNotificationModel SmsNotModel { get; set; }
public Guid SmsNotificationModelId { get; set; }
}
此外,我也创建了类public
。我不确定这是否重要,因为我相信Entity Framework需要从其自己的程序集访问我们的程序集。
然后,配置OnModelCreating
。
public class YourContext : DbContext
{
// ...
public DbSet<SmsNotificationModel> Notifications { get; set; }
public DbSet<SmsBody> SmsBodies { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
// ...
builder.Entity<SmsNotificationModel>(entity =>
{
// Configures the One-To-One relationship
// and the OnDelete behavior, as well.
entity.HasOne<SmsBody>(notification => notification.Body)
.WithOne(body => body.SmsNotModel)
.HasForeignKey<SmsBody>(body => body.SmsNotificationModelId)
.OnDelete(DeleteBehavior.Cascade);
});
}
}
而且,为了完整起见,数据访问层。我不知道通用约束是否适合Entity Framework策略。我相信如果可以。我只想提一下我要走的路。
public interface IRepository<TEntity> : IDisposable
{
// CRUD methods...
void Delete(TEntity entity);
void Delete(Expression<Func<TEntity, bool>> expression);
void Save();
}
我希望有一个定义所有CRUD操作的接口。即使是IAsyncRepository
,它定义了相同的操作,但是是异步的。
public class SmsRepository : IRepository<SmsNotificationModel>
{
private readonly YourContext _context;
public SmsRepository(YourContext context) => _context = context;
// CRUD operations implementation ...
public void Delete(SmsNotificationModel entity)
{
_context.Notifications.Remove(entity);
}
}
如果遇到任何麻烦,您可以继续阅读extended explanation。
希望有帮助。