Gengzu
Gengzu

Reputation: 542

Entity Framework 4.1 Code First and One-to-Many mapping problem

I have problem with mapping existing database.

2 tables (simplified)

"SomeEntity"
Id int
Name nvarchar

and

"EntityProperty"
EntityId int
Name nvarchar

and have relation one-to-many from Entity to entity properties.

How I can map this using EF 4.1 Code First?

Thx in advance.

Edited 1:

ok) this is my code

class Program
    {
        static void Main(string[] args)
        {
            var context = new DataContext();

            var result = context.SomeEntity.Include(p => p.EntityProperties);

            foreach (var entity in result)
            {
                Console.WriteLine(entity);
            }

        }
    }

    public class SomeEntity
    {
        public int EntityId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<EntityProperty> EntityProperties { get; set; }

        public override string ToString()
        {
            return string.Format("Id: {0}, Name: {1}", EntityId, Name);
        }
    }

    public class EntityProperty
    {
        public int EntityId { get; set; }
        public string Name { get; set; }
    }

    public class DataContext : DbContext
    {
        public DbSet<SomeEntity> SomeEntity { get { return this.Set<SomeEntity>(); } }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<SomeEntity>().ToTable("SomeEntity");
            modelBuilder.Entity<SomeEntity>().HasKey(k => k.EntityId);

            modelBuilder.Entity<EntityProperty>().ToTable("EntityProperty");
            modelBuilder.Entity<EntityProperty>().HasKey(k => k.EntityId);
        }
    }

Problem when I use Include in query for get properties:

Invalid column name 'SomeEntity_EntityId'. Invalid column name 'SomeEntity_EntityId'.

Upvotes: 4

Views: 14269

Answers (2)

Gengzu
Gengzu

Reputation: 542

I solved problem. I cannot add simple PK to relational table. I added complex PK on all unique fields and map one-to-many. That's all.

Upvotes: 1

Damb
Damb

Reputation: 14600

public class SomeEntity
{
    public int SomeEntityId {get;set;}
    public string Name {get;set;}
    public ICollection<EntityProperty> EntityProperties {get;set;}
}

public class EntityProperty
{
    public int EntityPropertyId {get;set;}
    public string Name {get;set;}
}

Creating that ICollection (at '1' side of relation) should be enough to set 1:N relation. It will create SomeEntity_Id (or SomeEntityId) column in EntityProperty table.

Edit: Btw: You can set that collection to virtual, if you want lazy loading enabled.

public virtual ICollection<EntityProperty> EntityProperties {get;set}

Edit:

public class SomeEntity
{
    [Key]
    public int Id {get;set;}
    public string Name {get;set;}
}

public class EntityProperty
{
    // What is PK here? Something like:
    [Key]
    public int Id {get;set;}

    // EntityId is FK
    public int EntityId {get;set;}

    // Navigation property
    [ForeignKey("EntityId")]
    public SomeEntity LinkedEntity {get;set;}

    public string Name {get;set;}
}

First try this.. then you can add that ICollection again, I didn't include it this time to keep it simple (and you an still query properties.. but with: context.EntityProperties.Where(x=>x.EntityId == X);)

Upvotes: 3

Related Questions