属性外键不能设置为null
我有4个表
public class StokGroup:BaseModel
{
public string GroupName { get; set; }
public List<GroupFeature> GroupFeatures { get; set; }
public List<StokCard> StokCards { get; set; }
}
public class GroupFeature : BaseModel
{
public int StokGroupId { get; set; }
public StokGroup StokGroup { get; set; }
public string FeatureName { get; set; }
public List<StokCardAdditionalFeatureValue> StokCardAdditionalFeatureValues { get; set; }
}
public class StokCard : BaseModel
{
public string IPMCode { get; set; }
public int StokGroupId { get; set; }
public StokGroup StokGroup { get; set; }
.......................................
}
public class StokCardAdditionalFeatureValue:BaseModel
{
public int StokCardId { get; set; }
public StokCard StokCard { get; set; }
public int GroupFeatureId { get; set; }
public GroupFeature GroupFeature { get; set; }
public string Value { get; set; }
}
,我的basemodel是具有主键ID的类,所有类都从基本模型继承
public class BaseModel
{
[Key]
public int Id { get; set; }
}
,我也有:
modelBuilder.Entity<StokCard>()
.HasOne(o => o.StokGroup)
.WithMany(m => m.StokCards)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<GroupFeature>()
.HasOne(o => o.StokGroup)
.WithMany(m => m.GroupFeatures);
modelBuilder.Entity<StokCardAdditionalFeatureValue>()
.HasOne(o => o.StokCard)
.WithMany(m => m.StokCardAdditionalFeatureValues)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<StokCardAdditionalFeatureValue>()
.HasOne(o => o.GroupFeature)
.WithMany(m => m.StokCardAdditionalFeatureValues);
现在问题是在删除stokgroup时自动删除Stokcard,因为这种关系是因为这种关系 我该如何解决这个问题?
我将null设置为stokgroupid在Stokcards桌子上,但迁移失败了,
您介意提供帮助吗?
提前致谢
I have 4 tables
public class StokGroup:BaseModel
{
public string GroupName { get; set; }
public List<GroupFeature> GroupFeatures { get; set; }
public List<StokCard> StokCards { get; set; }
}
public class GroupFeature : BaseModel
{
public int StokGroupId { get; set; }
public StokGroup StokGroup { get; set; }
public string FeatureName { get; set; }
public List<StokCardAdditionalFeatureValue> StokCardAdditionalFeatureValues { get; set; }
}
public class StokCard : BaseModel
{
public string IPMCode { get; set; }
public int StokGroupId { get; set; }
public StokGroup StokGroup { get; set; }
.......................................
}
public class StokCardAdditionalFeatureValue:BaseModel
{
public int StokCardId { get; set; }
public StokCard StokCard { get; set; }
public int GroupFeatureId { get; set; }
public GroupFeature GroupFeature { get; set; }
public string Value { get; set; }
}
And my BaseModel is the class that has Primary Key ID all classes inherit from BaseModel
public class BaseModel
{
[Key]
public int Id { get; set; }
}
And I have also :
modelBuilder.Entity<StokCard>()
.HasOne(o => o.StokGroup)
.WithMany(m => m.StokCards)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<GroupFeature>()
.HasOne(o => o.StokGroup)
.WithMany(m => m.GroupFeatures);
modelBuilder.Entity<StokCardAdditionalFeatureValue>()
.HasOne(o => o.StokCard)
.WithMany(m => m.StokCardAdditionalFeatureValues)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<StokCardAdditionalFeatureValue>()
.HasOne(o => o.GroupFeature)
.WithMany(m => m.StokCardAdditionalFeatureValues);
Now the problem is when the stokgroup is deleted StokCard is deleted automatically because of the relationship
How can I achieve this problem ?
I set NULL to StokGroupId on StokCards table but migration FAILED
Would you mind please help ?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论