一对多的代码优先引用

一对多的代码优先引用

我有以下两个表:

本土化

Id                     int
Text                   string

晚餐
Id                     int
Name                   string
Description            string
Name_LocalizationID    int
Description_LocationID int

现在,我希望我的POCO像这样:
public class Diner{
   public int Id{get;set;}
   public ICollection<Localization> NameLocalization{get;set;}
   public ICollection<Localization> DescriptionLocalization{get;set;}
}

public class Localization{
   public int Id{get;set;}
   public string Text{get;set;}
}

问题是:如何使用EF Fluent API将NameLocalization和DescriptionLocalization属性映射到本地化ID?
谢谢

最佳答案

SQL Server不支持多个级联删除,因此需要从您的对象模型中派生一种关联,以便派生SQL Server架构:

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");
        });
}

关于entity-framework - 一对多的代码优先引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4939331/

10-09 02:37