我正在尝试实现实体框架代码优先方法,但是我收到错误:
引入FOREIGN KEY约束 ' FK_dbo.StateMasters_dbo.CountryMasters_CountryRefId'桌子上 ' StateMasters'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY 约束
这是我的EF代码。只需在数据库中插入记录。
域类:
public class CountryMaster
{
public CountryMaster()
{
}
[Key]
public int CountryCode { get; set; }
[ConcurrencyCheck, Required, MaxLength(50), MinLength(2)]
public string CountryName { get; set; }
public ICollection<StateMaster> States { get; set; }
public ICollection<CityMaster> Cities { get; set; }
}
public class StateMaster
{
public StateMaster()
{
}
[Key]
public int StateCode { get; set; }
public int CountryRefId { get; set; }
[ForeignKey("CountryRefId")]
public CountryMaster Country { get; set; }
[ConcurrencyCheck, Required, MaxLength(50), MinLength(2)]
public string StateName { get; set; }
// public ICollection<CityMaster> Cities { get; set; }
}
public class CityMaster
{
public CityMaster()
{
}
[Key]
public int CityCode { get; set; }
public int CountryRefId { get; set; }
[ForeignKey("CountryRefId")]
public CountryMaster Country { get; set; }
public int StateRefId { get; set; }
[ForeignKey("StateRefId")]
public StateMaster State { get; set; }
[ConcurrencyCheck, Required, MaxLength(50), MinLength(2)]
public string CityName { get; set; }
}
存储库界面:
public interface I_COUNTRY_STATE_CITYRepository:IDisposable
{
void InsertCountry(CountryMaster Country);
void SaveCountry();
}
存储库实现:
public class COUNTRY_STATE_CITYRepository : IDisposable, I_COUNTRY_STATE_CITYRepository
{
private COUNTRY_STATE_CITYContext context;
public COUNTRY_STATE_CITYRepository(COUNTRY_STATE_CITYContext context)
{
this.context = context;
}
public void InsertCountry(CountryMaster Country)
{
context.Countries.Add(Country);
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void SaveCountry()
{
context.SaveChanges();
}
}
上下文类:
public class COUNTRY_STATE_CITYContext: DbContext
{
public COUNTRY_STATE_CITYContext() : base("ConnectionString")
{ }
public DbSet<CountryMaster> Countries { get; set; }
public DbSet<StateMaster> States { get; set; }
public DbSet<CityMaster> Cities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<CountryMaster>().HasMany(i => i.States).WithRequired().WillCascadeOnDelete(false);
}
}
C#代码只是传递国家/地区名称的值并插入数据库:
public partial class CheckCountryStateCity : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void btnAdd_Click(object sender, EventArgs e)
{
using (var ctx = new COUNTRY_STATE_CITYContext())
{
COUNTRY_STATE_CITYRepository wr = new COUNTRY_STATE_CITYRepository(ctx);
CountryMaster wt = new CountryMaster();
wt.CountryName = "ABC";
wr.InsertCountry(wt);
wr.SaveCountry();
}
}
}
答案 0 :(得分:0)
在某些表中,您有使用外键的现有记录。因此,当您尝试删除此类数据时,会导致错误。
解决方案: 如果要删除父记录删除时引用的数据,则应在数据库中提及级联删除。
OR
您必须在删除引用记录时限制用户。