C# 实体框架代码中的一对一关系优先
我首先使用的是C#EF代码。我有以下两门课: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
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);