christo
christo

Reputation: 701

The Entity Framework created own columns for foreign keys

The Entity Framework ignores my foreign keys and creates own. How I can solve this problem?

I have few classes:

[Table("Entity")]
public class Entity
{
    public Entity()
    {
        HeavyEntities = new List<HeavyEntity>();
    }

    [Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int EntityId { get; set; }

    public string Name { get; set; }

    public ICollection<HeavyEntity> HeavyEntities { get; set; }
}   

[Table("HeavyEntity")]
public class HeavyEntity
{
    [Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int EntityHeavyId { get; set; }

    public int EntityId { get; set; }
    public virtual Entity Entity { get; set; }

    public int? TargetEntityId { get; set; }
    public virtual Entity TargetEntity { get; set; }

    public Relation Relation { get; set; }
}

[Table("Tech")]
public class Tech : Entity
{
    public string Status { get; set; }
}

[Table("Company")]
public class Company : Entity
{
    public string Country { get; set; }
}

Create and save entities:

var (context = new MyDbContex())
{
    var tech = new Tech
    {
        Name = "Tech1",
        Status = "New",
    };

    var company = new Company
    {
        Name = "Company1",
        Country = "Ukraine"
    };

    tech.HeavyEntities.Add(new HeavyEntity
    {
        Relation = Relation.AsParent,
        TargetEntity = company,
    });

    context.Techs.Add(tech);
    context.Companies.Add(company);

    context.SaveChanges();
}

For this classes Entity Framework creates the following table (HeavyEntity):

Result table

How I can map foreign keys to my fields?

SOLVED:

[Table("Entity")]
public class Entity
{
    public Entity()
    {
        HeavyEntities = new List<HeavyEntity>();
    }

    [Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int EntityId { get; set; }

    public string Name { get; set; }

    [InverseProperty("Entity")]
    public ICollection<HeavyEntity> HeavyEntities { get; set; }
}   

[Table("HeavyEntity")]
public class HeavyEntity
{
    [Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int EntityHeavyId { get; set; }

    [ForeignKey("EntityId")]
    public int EntityId { get; set; }
    public virtual Entity Entity { get; set; }

    [ForeignKey("TargetEntityId")]
    public int? TargetEntityId { get; set; }
    public virtual Entity TargetEntity { get; set; }

    public Relation Relation { get; set; }
}

Upvotes: 0

Views: 31

Answers (1)

phil soady
phil soady

Reputation: 11348

Add the ForeignKey attribute.

see https://msdn.microsoft.com/en-us/data/jj591583#Relationships

[Table("HeavyEntity")]
public class HeavyEntity
{
[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int EntityHeavyId { get; set; }

public int EntityId { get; set; }
public virtual Entity Entity { get; set; }

public int? TargetEntityId { get; set; }
[ForeignKey("TargetEntityId")]     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public virtual Entity TargetEntity { get; set; }

public Relation Relation { get; set; }

}

Upvotes: 1

Related Questions