我在颜色和库存表之间有一对多的关系。
我的课程: Color.cs:
public class Color:Base.BaseEntity
{
public string ColorName { get; set; }
public virtual IList<InventoryEntity.Inventory> Inventories { get; set; }
}
Inventory.cs:
public class Inventory : Base.BaseEntity
{
public int VariationId { get; set; }
public int ColorId { get; set; }
public int Quantity { get; set; }
public VariationEntity.Variation OwnerVariation { get; set; }
public ProductEntity.Color OwnerColor { get; set; }
}
我的背景:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
#region Inventory-Colors
modelBuilder.Entity<Entity.InventoryEntity.Inventory>()
.HasRequired(m => m.OwnerColor)
.WithMany(t => t.Inventories)
.HasForeignKey(m => m.ColorId)
.WillCascadeOnDelete(false);
#endregion
}
我的问题:
string CName = rpinventory.FirstOrDefault(x => x.Id == 1).OwnerColor.ColorName;
它总是为空。代码可以找到id = 1的库存,但ownercolor为null。
我无法弄清楚原因。有什么我想念的吗?我有同样的方式的另一种关系。他们工作正常。