0

So I have to import a table from another database. The table is self referencing using it's own key, but I can't trust the key, especially since the table I'm importing into will be for multiple datasets.

I'm using entity framework and code first fluent api, but having a bugger of a time finding a workaround to this issue.

Here is my POCO class (simplified to only the data points needed):

public class Person
{
    public virtual int Id { get; set; }
    public virtual int personid { get; set; }
    public virtual int? parentid { get; set; }

    public virtual Person parent { get; set; }
    public virtual ICollection<Person> children { get; set; }
}

This is would be a simple self referencing table, if I could use Id, but I have to use personid. I have setup the idea of the fluent api that I need here, but EF keeps looking at Id.

modelBuilder.Entity<Person>()
   .HasOptional(p => p.parent)
   .WithMany(p => p.children)
   .HasForeignKey(p => p.parentid)
   .WillCascadeOnDelete(false);

Any workaround ways to fill the collection using the personid as a Unique Constraint (that is not yet implemented in EF6.1.2)?

I am also using Asp.NetBoilerplate, so some control over my entities, repositories, etc. are possible, but not recommended. I.e. 'Id' is assumed to be the Primary Key for any and all tables.

Salteris
  • 201
  • 2
  • 6

1 Answers1

0

hmm..

why not use the HasKey to define the id column??

modelBuilder.Entity<Person>()
    .HasKey(p=> p.personId)
    .HasOptional(p => p.parent)
    .WithMany(p => p.children)
    .HasForeignKey(p => p.parentid)
    .WillCascadeOnDelete(false);
amuz
  • 334
  • 2
  • 11
  • That doesn't seem to work. When run, Entity Framework errors out that it is looking for the Person_personid column and that column name doesn't exist. I could rename the column in the table to Person_personid, but then I would have to refactor my importing tool... – Salteris Dec 11 '14 at 00:50
  • hmm.. i just tested it with a sample code and seems to work fine. Used your sample class added the mappings as above and created two people parent and child and no error.. everything is correctly stored.. – amuz Dec 11 '14 at 01:06
  • OK, then this is a ABP problem. I have forwarded it on to the creator to see if there is anything already in the code to handle this situation. I'll post any answer he might give, thanks. – Salteris Dec 11 '14 at 15:59