C#ef地图实体

时间:2015-07-31 12:08:12

标签: c# entity-framework

我有3个实体:

  public class Category
{
    public Category()
    {
        this.Companies = new Company();
        this.Language = new Language();
    }

    public string Id { get; set; }
    public string Name { get; set; }
    public string ParentId { get; set; }
    public int Sequence { get; set; }
    public string Company { get; set; }
    public virtual Company Companies { get; set; }
    public int CompanyId { get; set; }
    public virtual Language Language { get; set; }
    public string LanguageId { get; set; } 
 }

  public class Company
{
    public Company()
    {
        this.Categories = new List<Category>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
}


     public class Language
{
    public Language()
    {
        this.Categories = new List<Category>();
    }

    public string Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
}

然后我在类别上有我的地图:

    public class CategoryMap : EntityTypeConfiguration<Category>
{
    public CategoryMap()
    {
        //Primary Key
        this.HasKey(w => new { w.Id, w.LanguageId, w.Company} );

        //Properties
        this.Property(w => w.Id)
            .HasMaxLength(50)
            .IsRequired();

        this.Property(w => w.Name)
            .HasMaxLength(100);

        this.Property(w => w.ParentId)
            .HasMaxLength(50);

        this.Property(w => w.Sequence)
            .IsRequired();

        this.Property(w => w.Company)
            .HasMaxLength(50)
            .IsRequired();

        this.Property(w => w.CompanyId)
            .IsRequired();

        this.Property(w => w.LanguageId)
            .IsRequired();

        //Table and columns mappings
        this.ToTable("Category");
        this.Property(w => w.Id).HasColumnName("id");
        this.Property(w => w.Name).HasColumnName("name");
        this.Property(w => w.ParentId).HasColumnName("parentid");
        this.Property(w => w.Sequence).HasColumnName("Sequence");
        this.Property(w => w.CompanyId).HasColumnName("CompanyId");
        this.Property(w => w.Company).HasColumnName("Company");
        this.Property(w => w.LanguageId).HasColumnName("languageid");

        //Relastionships
        this.HasRequired(w => w.Companies)
            .WithMany(y => y.Categories)
            .HasForeignKey(w => w.CompanyId);
        this.HasRequired(w => w.Language)
            .WithMany(w => w.Categories)
            .HasForeignKey(w => w.LanguageId);
    }
   }

然后在我的存储库中我这样做:

  var translatedCategory = ctx.Category.Include(w => w.Companies).Include(w => w.Language).First(w => w.Id == category.Id);

我希望它能找到公司和语言,但它们总是一个新的对象。

enter image description here

那有什么不对?为什么不找到与该实体相关的公司?

1 个答案:

答案 0 :(得分:0)

只是一个建议:你已经在他们之间建立了关系。你不需要使用.Include。

var translatedCategory = ctx.Category.Where(x => x.Companies.Id == someID);