Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架代码中的一对一关系优先_C#_C# 4.0_Entity Framework 6 - Fatal编程技术网

C# 实体框架代码中的一对一关系优先

C# 实体框架代码中的一对一关系优先,c#,c#-4.0,entity-framework-6,C#,C# 4.0,Entity Framework 6,我首先使用的是C#EF代码。我有以下两门课: 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 virtu

我首先使用的是C#EF代码。我有以下两门课:

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关系。这样我就可以从
国家获得
货币
详细信息,并且可以从
货币
获取
国家
详细信息

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

 modelBuilder
        .Entity<Om_Currency>()
        .HasOptional(f => f.Country)
        .WithRequired(s => s.Currency);
这是
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);
    }
}
公共类CurrencyMap:EntityTypeConfiguration
{
公共货币地图()
{
属性(x=>x.CurrencyID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
属性(x=>x.CurrencySymbol)
.IsRequired()
.IsVariableLength()
.HasMaxLength(50)
.HasColumnAnnotation
(
IndexAnnotation.AnnotationName,
新指数注释法
(
新的索引属性(“U_CurrencySymbol”,1){IsUnique=true}
)
);
属性(x=>x.IsActive).IsRequired();
属性(x=>x.CountryID).IsRequired();
ToTable(共有TBL电流);
}
}

我解决了这个问题

而不是低于

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);
modelBuilder
.实体()
.has可选(f=>f.Currency)
.需要(s=>s.Country);
建模者
.实体()
.has可选(f=>f.Country)
.需要(s=>s.货币);
它应该在下面

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);
modelBuilder.Entity()
.HasRequired(x=>x.Currency).WithMany()
.HasForeignKey(x=>x.CurrencyID).WillCascadeOnDelete(false);
modelBuilder.Entity()
.HasRequired(x=>x.Country).WithMany()
.HasForeignKey(x=>x.CountryID).WillCascadeOnDelete(false);