Concerning the relationship configuration of Entity Framework, the error message "invalid column name XXXX_Id" is displayed. entityxxxx_id

Source: Internet
Author: User

Concerning the relationship configuration of Entity Framework, the error message "invalid column name XXXX_Id" is displayed. entityxxxx_id

Problem description:

There are two tables in the database:

The Member table has a foreign key RoleId, which corresponds to the primary key Id of the Role table, the business logic is that the RoleId of the Member table must correspond to the Id of the Role table (but no foreign key is set when designing the data table, which is configured using EF (Entity Framework, the same below)

The code for homepage Member is as follows:

    public partial class Member    {        public int Id { get; set; }        public string Name { get; set; }        public string Password { get; set; }        public bool Delete{ get; set; }        public int RoleId { get; set; }        public virtual Role Role { get; set; }    }
View Code

The code for the Role table is as follows:

    public partial class Role    {        public int Id { get; set; }        public string Name { get; set; }        public virtual IList<Member> Members { get; set; }    }
View Code

The relationship code of Member and Role is as follows:

public class MemberMap : EntityTypeConfiguration<Member>    {        public MemberMap()        {            this.ToTable("Member");            this.HasKey(m => m.Id);            this.HasRequired(m => m.Role).WithMany().HasForeignKey(m => m.RoleId);        }    }
View Code
    public class RoleMap: EntityTypeConfiguration<Role>    {        public RoleMap()        {            this.ToTable("Role");            this.HasKey(r => r.Id);        }    }
View Code

The EF operation class is as follows:

public partial class EFContext<T> : DbContext where T : class    {        public EFContext(): base("name=MyConnectionString")        {        }        protected override void OnModelCreating(DbModelBuilder modelBuilder)        {            Database.SetInitializer<EFContext<T>> (null);            modelBuilder.Configurations.Add(new MemberMap());            modelBuilder.Configurations.Add(new RoleMap());            base.OnModelCreating(modelBuilder);        }        public DbSet<T> Table { get; set; }        public IQueryable<T> GetList(Expression<Func<T,bool>> where)        {            return this.Table.Where(where);        }    }
View Code

An exception is prompted when the program is running,

 

 

The details are as follows:

Use SQL Profiler to find the generated code as follows:

 

The actual RoleId is a foreign key.

The solution is to configure the witing with WithMany (), that is, modify the MemberMap as follows:

public class MemberMap : EntityTypeConfiguration<Member>    {        public MemberMap()        {            this.ToTable("Member");            this.HasKey(m => m.Id);            this.HasRequired(m => m.Role).WithMany(r => r.Members).HasForeignKey(m => m.RoleId);        }    }

Then run the console program. There is no exception. The SQL Profiler is as follows:

 

Someone may ask, what if we do use withbers () instead of withbers (r => r. Members) in MemberMap?

The modification method is as follows:

Comment out or delete the Members attribute in the Role entity code

public partial class Role    {        public int Id { get; set; }        public string Name { get; set; }        //public virtual IList<Member> Members { get; set; }    }
View Code

Modify MemberMap as described above:

public class MemberMap : EntityTypeConfiguration<Member>    {        public MemberMap()        {            this.ToTable("Member");            this.HasKey(m => m.Id);            this.HasRequired(m => m.Role).WithMany().HasForeignKey(m => m.RoleId);        }    }
View Code

Finally, re-run the program without exception.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.