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

  • Home
  • SEARCH
  • 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 6088243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:57:46+00:00 2026-05-23T11:57:46+00:00

I am new to Entity Framework and hence this question may seem a little

  • 0

I am new to Entity Framework and hence this question may seem a little noobish.

I will try to explain my scenario with he Department-Employee example I have two tables “Department” and “Employee”. Department has an identity column DeptID. I am trying to create a new Department and add newly created Employees to it all in one go. Below is my code:

using (MyDB context = new MyDB())
{
Department dept = new Department(); 
dept.Name = "My Department"; 

Employee emp = new Employee(); 
emp.Name = "Emp Name"; 
emp.Department = dept; //Tried dept.Employees.Add(emp) also, same result
context.AddObject("Department", dept);
context.SaveChanges()
}

But for some reason, the record doesn’t get inserted. It throws an error in the second insert query.

Below are the queries:

INSERT INTO Department
           (Name)
VALUES     ('Dept1' /* @gp1 */);
SELECT ID
FROM   Department
WHERE  row_count() > 0
AND `ID` = last_insert_id()


--------------------------
INSERT INTO Employee
           (DeptID,
           Name)
VALUES     (19,
           'Name'); /* @gp1 */
SELECT id
FROM   Employee
WHERE  row_count() > 0
AND `id` = last_insert_id()

The error it throws is at line 4 of second query. So I am guessing something is wrong with the Identity thing. I am using MySQL.

Can anyone please explain what could be wrong?

EDIT: I have modified the SQL to suit this example. I can’t give my real table details.

  • 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-05-23T11:57:47+00:00Added an answer on May 23, 2026 at 11:57 am

    What is the structure of your classes? I’d assume there is something slightly wrong and EF isn’t correctly building the model.

    Also I had some problems with EF4.1 until I manually defined the key. The part of EF that ‘assumes’ which variable is your key doesn’t seem to work on some complex objects like objects that are derived from a base class and also fails in other cases.

    Here is what I would expect your code to look like:

    public class Department
    {
        [Key]
        public Int64 DepartmentId { get; set;}
    
        public String Name { get; set;}
    }
    
    public class Employee
    {
        [Key]
        public Int64 EmployeeId { get; set;}
    
        public String Name { get; set;}
    
        //Adding virtual here allows lazy loading of department
        public virtual Department Department {get; set;}
    }
    
    
    public class MyDatabase : DbContext
    {
        DbSet<Department> Departments;
        DbSet<Employee> Employees;
    }
    

    I have an entire project at work that relies on Entity framework correctly mapping the above into a Many to One relationship and I’ve had no issues using code just as shown.

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

Sidebar

Related Questions

I read that the new Entity Framework will include a method to delete multiple
I created a new Entity Framework model from a database. This database has a
This is entity framework 4. CurrentProperty.FMVHistories.Add(FMVPresenter.GetFMVHistoryObject()); DataLayer.AccrualTrackingEntities repository = new AccrualTrackingEntities(); repository.Properties.AddObject(CurrentProperty); repository.SaveChanges(); Right
I am very new to the entity framework, so please bear with me... How
I'm confused about Linq to Entities. Is it the new name of Entity Framework
I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it)
I'm planning to use MS entity framework for new web apps (come on EF
I heard about the new entity framework for .net, and decided to modify my
I'm new to Entity Framework and am trying to figure things out. I have
I've been looking at the new Entity Framework 4 Code Only features, and I

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.