【发布时间】:2016-10-03 07:17:27
【问题描述】:
我的实体框架有问题我有两个代码一流的国家和城市
[Table("dbo.Countries")]
public class Country
{
public int CountryId { get; set; }
public string CountryNameAr { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
[Table("dbo.Cities")]
public class City
{
public int CityId { get; set; }
public int CountryId { get; set; }
public string CityNameAr { get; set; }
public virtual Country Country { get; set; }
}
每个国家都有很多城市,我已经添加了一些国家和城市。 我的问题是:当我删除任何国家时,它会将城市中的 countryId 更新为 null。 我已经在 DBContext 中写过:
ModelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
ModelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
当我在 SQL Server 中跟踪它时...实体框架在城市表中创建更新语句然后在国家表中删除语句...
exec sp_executesql N'UPDATE [dbo].[Cities]
SET [CountryId] = NULL
WHERE ([CityId] = @0)
exec sp_executesql N'DELETE dbo.Countries
WHERE (CountryId = @0)',N'@0 int',@0=6
我想停止这个..如果实体框架与任何表相关,我希望实体框架拒绝删除 有谁知道怎么解决这个问题???
【问题讨论】:
-
不要让你的完整逻辑基于Entity Framework FK依赖,你应该在删除任何值之前检查依赖。
-
我将如何检查 plz 因为我首先使用代码???我如何根据实体框架 FK 依赖项制作完整的逻辑
标签: c# asp.net .net asp.net-mvc entity-framework