2015-09-20 2 views
0

Я использую сначала код C# EF. У меня есть следующие 2 класса:Отношения «один-к-одному» в Entity Framework с кодовым кодом

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

Теперь я пытаюсь реализовать соотношение 1-1 между этими двумя классами. Так что я могу получить Currency Детали от Country и Country Детали могут быть получены от Currency.

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:

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

и это класс отображения для 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); 
    } 
} 

ответ

0

Я понял вопрос. 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); 
Смежные вопросы