开发者

Column mapping with Abstract class (TPT with existing tables)

开发者 https://www.devze.com 2023-04-09 14:05 出处:网络
I am developing against existing DB, having problem mapping my abstract class to my table as TPT., which raised an error saying \"invalid column name \"ID\"\".

I am developing against existing DB, having problem mapping my abstract class to my table as TPT., which raised an error saying "invalid column name "ID"".

Simple class as follows

public abstract class Member 
{
    [DisplayName("ID")]
    public string ID { get; set; }

    [DisplayName("Date Applied")]
    public System.DateTime? DateApplied { get; set; }

    [DisplayName("Date Membered")]
    public System.DateTime? DateMembered { get; set; }

    [DisplayName("Member Type")]
    public int MemberTypeFlag { get; set; }

}


public class Person : Member
{
    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Date Of Birth")]
    public System.DateTime DateOfBirth { get; set; }
}

And mapped to my custom table as follows

public class MapMember : EntityTypeConfiguration<Member>
{
    public MapMember()
        : base()
开发者_开发问答    {
        HasKey(b => b.ID).Property(b => b.ID).HasColumnName("ID");
        Property(b => b.DateApplied).HasColumnName("DTM_APPLIED");
        Property(b => b.DateMembered).HasColumnName("DTM_MEMBERED");
        Property(b => b.MemberTypeFlag).HasColumnName("MBR_TYPE_FLG");
        ToTable("MBR");
    }
}

public class MapPerson : EntityTypeConfiguration<Person>
{
    public MapPerson()
        : base()
    {
        Property(p => p.LastName).HasColumnName("LNAME");
        Property(p => p.FirstName).HasColumnName("FNAME");
        Property(p => p.DateOfBirth).HasColumnName("DOB");
        Property(p => p.FirstName).HasColumnName("FNAME");
        ToTable("MBR_PERSON");
    }
}

However Invalid column name "ID" raised upon db.savechanges().

Please advise.

Thanks


This error will occur if the primary key column in your MBR_PERSON table (which must be foreign key to the MBR table at the same time) does not have the column name ID (the name of the key column in table MBR). In TPT mapping the primary key columns of the root table for the abstract entity must have the same name as the primary key columns of the tables for the derived entities.

0

精彩评论

暂无评论...
验证码 换一张
取 消