Entity Framework 代码优先中的一对一关系

One-to-one relationship in Entity Framework code-first

我正在使用 C# EF 代码优先。我有以下 2 classes:

public class Om_Currency
{
    [Key]
    public Int16 CurrencyID { get; set; }
    public String CurrencySymbol { get; set; }
    public Boolean IsActive { get; set; }

    public Int32 CountryID { get; set; }

    public virtual Om_Country Country { get; set; }
}

public class Om_Country
{
    [Key]
    public Int16 CountryID { get; set; }
    public String CountryName { get; set; }
    public Boolean IsActive { get; set; }

    public Int32 CurrencyID { get; set; }

    public virtual Om_Currency Currency { get; set; }
}

现在,我正在尝试在这两个 class 之间实现一对一的关系。这样我就可以从 Country 获取 Currency 详细信息,并且可以从 Currency.

获取 Country 详细信息
 modelBuilder
        .Entity<Om_Country>()
        .HasOptional(f => f.Currency)
        .WithRequired(s => s.Country);

 modelBuilder
        .Entity<Om_Currency>()
        .HasOptional(f => f.Country)
        .WithRequired(s => s.Currency);

但是我得到这个错误:

The navigation property 'Country' declared on type 'ObjectModel.Country.Om_Currency' has been configured with conflicting multiplicities.

我是不是做错了什么?

这是 Country 的映射 class:

public class CountryMap : EntityTypeConfiguration<Om_Country>
{
    public CountryMap()
    {
        Property(x => x.CountryID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.CountryName)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnAnnotation
            (
                IndexAnnotation.AnnotationName,
                new IndexAnnotation
                    (
                        new IndexAttribute("U_CountryName", 1) { IsUnique = true }
                    )
           );
        Property(x => x.IsActive).IsRequired();
        Property(x => x.CurrencyID).IsRequired();
        ToTable(clsCommon.tblCountry);
    }
}

这是 class 对应 Currency 的映射:

public class CurrencyMap : EntityTypeConfiguration<Om_Currency>
{
    public CurrencyMap()
    {
        Property(x => x.CurrencyID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.CurrencySymbol)
            .IsRequired()
            .IsVariableLength()
            .HasMaxLength(50)
            .HasColumnAnnotation
            (
                IndexAnnotation.AnnotationName,
                new IndexAnnotation
                    (
                        new IndexAttribute("U_CurrencySymbol", 1) { IsUnique = true }
                    )
            );

        Property(x => x.IsActive).IsRequired();
        Property(x => x.CountryID).IsRequired();
        ToTable(clsCommon.tblCurrency);
    }
}

我想通了这个问题。 I got the Solution from here

而不是下面

modelBuilder
    .Entity<Om_Country>()
    .HasOptional(f => f.Currency)
    .WithRequired(s => s.Country);

modelBuilder
    .Entity<Om_Currency>()
    .HasOptional(f => f.Country)
    .WithRequired(s => s.Currency);

应该在下面

modelBuilder.Entity<Om_Country>()
    .HasRequired(x => x.Currency).WithMany()
    .HasForeignKey(x => x.CurrencyID).WillCascadeOnDelete(false);

modelBuilder.Entity<Om_Currency>()
    .HasRequired(x => x.Country).WithMany()
    .HasForeignKey(x => x.CountryID).WillCascadeOnDelete(false);