Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:47:13+00:00 2026-06-14T02:47:13+00:00

I have these schema in my database: Tb1: { Id:int , NameTb1:varchar(50) } Tb2:

  • 0

I have these schema in my database:

Tb1: { Id:int , NameTb1:varchar(50) }
Tb2: { Id:int , NameTb2:varchar(50) }
Tb1Tb2 { Tb1Id:int , Tb2Id:int }

Obviously Tb1Tb2 is a relationship table and I want to define a many-to-many relationship in EF code-first.

And these are the entity classes :

public class Tb1 
{
   public Tb1()
   {
       ListTb2 = new List<Tb2>();
   }
   public int Id { get; set; }
   public string NameTb1 { get; set; }

   public virtual ICollection<Tb2> ListTb2 { get; set; }
}

public class Tb2 
{
   public Tb2()
   {
       ListTb1 = new List<Tb1>();
   }
   public int Id { get; set; }
   public string NameTb2 { get; set; }

   public virtual ICollection<Tb1> ListTb1 { get; set; }
}

and mappings :

 public class Tb1Map : EntityTypeConfiguration<Tb1>
 {
    public Tb1Map()
    {
        this.HasKey(x => x.Id);

        this.HasMany(x => x.ListTb2)
            .WithMany(xx => xx.ListTb1)
            .Map
            (
                x =>
                    {
                        x.MapLeftKey("Tb1Id");
                        x.MapRightKey("Tb2Id");
                        x.ToTable("Tb1Tb2");
                    }
            );     
    }
}

public class Tb2Map : EntityTypeConfiguration<Tb2>
{
    public Tb2Map()
    {
        this.HasKey(x => x.Id);
    }
}

When I use it in my app :

var sv1 = new TableService<Tb1>(_uow);
var sv2 = new TableService<Tb2>(_uow);

var t1 = new Tb1 { NameTb1 = "T111" };
sv1.Add(t1);
//var res1= _uow.SaveChanges();

var t2 = new Tb2 { NameTb2 = "T222" };
sv2.Add(t2);
//var res2 = _uow.SaveChanges();

t1.ListTb2.Add(t2);
var result = _uow.SaveChanges();

I get this error:

An error occurred while saving entities that do not expose foreign key
properties for their relationships. The EntityEntries property will
return null because a single entity cannot be identified as the source
of the exception. Handling of exceptions while saving can be made
easier by exposing foreign key properties in your entity types. See
the InnerException for details.

and inner exception is:

The INSERT statement conflicted with the FOREIGN KEY constraint
“FK_Tb1Tb2_Tb2”. The conflict occurred in database “dbTest”, table
“dbo.Tb2”, column ‘Id’.

Why do I get this error?
and what is the solution?
tnx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:47:14+00:00Added an answer on June 14, 2026 at 2:47 am

    I remove the

    sv2.Add(t2);
    

    and it worked

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the database schema we have. t_RoleCombinations - These are all possible combination
I have a pretty complex database schema and would like to know if there
I have these related tables in my database: table [Files]: FileID FileName ------ --------
I have basic generalisation schema in database, I have superclass vehicle, and its subclases
I have a question about a database schema for a certain website where people
I am currently working on restructuring my site's database. As the schema I have
I have database schema for an integration project in which I need to be
(Not related to versioning the database schema) Applications that interfaces with databases often have
I have an existing MySQL database schema in production for an PHP5 application. The
I have a series of about 30 lookup tables in my database schema, all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.