【问题标题】:EF5 Fluent API mappingEF5 Fluent API 映射
【发布时间】:2012-12-01 12:47:19
【问题描述】:

我正在尝试映射这些实体:

 [Table("AAN_DigitalLib_Asset")]
    public class Asset
    {
    [Column("ID")]
    public int ID { get; set; }

    [Column("format_id")]
    public int FormatID { get; set; }

    [Column("person_id")]
    public int? PersonID { get; set; }

    [Column("publicationYear")]
    public int? PublicationYear { get; set; }

    public Person People { get; set; }

    //public virtual AssetKeyword AssetKeywords { get; set; }

    public virtual ICollection<AssetKeyword> AssetKeywords { get; set; }
}

使用这些实体:

[Table("AAN_DigitalLib_AssetKeyword")]
    public class AssetKeyword
    {
        [Column("ID")]
        public int ID { get; set; }

        [Column("asset_id")]
        public int AssetID { get; set; }

        [Column("keyword")]
        public string Keyword { get; set; }
    }

单个Asset 可以有多个AssetKeywords。数据库中没有指定关系,如何使用 Fluent API 创建关系?

【问题讨论】:

    标签: c# entity-framework asp.net-mvc-4 linq-to-entities entity-relationship


    【解决方案1】:

    添加

    [ForeignKey("AssetID")]
    public virtual Asset Asset { get; set; }
    

    在你的 AssetKeyword 类上修复它?

    如果不这样做,那么在您的 onModelCreating() 中执行此操作

    modelBuilder.Entity<AssetKeyword>()
                        .HasRequired(p => p.Asset)
                        .WithMany()
                        .HasForeignKey(p => p.AssetID);
    

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 1970-01-01
      • 2015-01-26
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多