Code-First Reference one-to-many

孤街醉人 提交于 2019-11-30 10:08:19

SQL Server does not support multiple cascade deletes, so you need to make one of your associations optional in order to derive a SQL Server schema from your object model:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Diner>()
        .HasRequired(diner => diner.NameLocalization)
        .WithMany()
        .IsIndependent()
        .Map(s => 
        { 
            s.MapKey(localization => localization.Id, "NameLocalizationID"); 
        });

    modelBuilder.Entity<Diner>()
        .HasOptional(diner => diner.DescriptionLocalization)
        .WithMany()
        .IsIndependent()
        .Map(s => 
        { 
            s.MapKey(localization => localization.Id, "DescriptionLocationID"); 
        });
}

I've not tested this code, I'm afraid, but maybe something like:

modelBuilder.Entity<Diner>()
    .HasRequired(diner => diner.NameLocalization)
    .WithMany().IsIndependent()
    .Map(s => { s.MapKey(localization => localization.Id, "Name_LocalizationID"); });

modelBuilder.Entity<Diner>()
    .HasRequired(diner => diner.DescriptionLocalization)
    .WithMany().IsIndependent()
    .Map(s => { s.MapKey(localization => localization.Id, "Description_LocationID"); });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!