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 8455855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:23:41+00:00 2026-06-10T12:23:41+00:00

I am learning the usage of POCO classes and relationships between entities. I have

  • 0

I am learning the usage of POCO classes and relationships between entities.

I have two tables. And I created Data Model, and POCO classes for them. But when I tried to add a new Vehicle entity and a new Journey at the same time before calling context.SaveChanges(), those two new entities will be associated automatically. That means, the new Journey record will have its VehicleId field populated with Id of the new Vehicle record.

But when I switched to entity classes generated by Data Model, that VehicleId will be null.

Here is the code I used to insert entities:

using (var context = new TravelEntities())
{
    var newVehicle = new Vehicle();
    newVehicle.Name = "Fly by yourself";
    context.Vehicles.AddObject(newVehicle);

    var newJourney = new Journey();
    newJourney.Location = "Wuyishan";
    context.Journeys.AddObject(newJourney);

    context.SaveChanges();
}

And the POCO classes and context class:

public class Vehicle
{
    public virtual int Id { get; set; }
    public virtual String Name { get; set; }
    public virtual ICollection<Journey> Journeys { get; set; }
}
public class Journey
{
    public virtual int Id { get; set; }
    public virtual String Location { get; set; }
    public virtual int VehicleId { get; set; }
    public virtual Vehicle Vehicle { get; set; }
}

class TravelEntities : ObjectContext
{
    ObjectSet<Vehicle> _vehicles;
    ObjectSet<Journey> _journeys;

    public TravelEntities()
        : base("name=TravelEntities", "TravelEntities")
    {
        _vehicles = CreateObjectSet<Vehicle>();
        _journeys = CreateObjectSet<Journey>();
    }

    public ObjectSet<Vehicle> Vehicles { get { return _vehicles; } }
    public ObjectSet<Journey> Journeys { get { return _journeys; } }
}

And following is the database schema:

create table Vehicle
(
    Id int identity(1,1) primary key,
    Name nvarchar(max)
)
go

create table Journey
(
    Id int identity(1,1) primary key,
    Location nvarchar(max),
    VehicleId int,
    constraint FK_Journey_Vehicle foreign key(VehicleId) references Vehicle(Id)
)
go

What I want to know is why that two new entities are associated, and how can I avoid this. Thanks for any help.

  • 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-10T12:23:43+00:00Added an answer on June 10, 2026 at 12:23 pm

    All right, Slauma gave the answer in his comment, but he didn’t come back. My comment for him in the question is hidden when the page is loaded, maybe he didn’t see it.

    The problem resides in the VehicleId property of Journey class, it should be nullable so that the default value is null for that newJourney. So I change the type of VehicleId as int? . When it is of type int, the default value is 0, which is equal to the Id value of newVehicle, I think that is the reason why they are correlated. This does not make sense, but that is how the EF works.

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

Sidebar

Related Questions

I'm learning Zend Framework, but I have some doubts about the usage and the
I have been watching Xcode tutorials on youtube and learning from examples online but
Just learning R. Given a data.frame in R with two columns, one numeric and
I'm learning to use neo4j , but am a bit confused on its usage.
I'm just starting out learning python (for webapp usage) but I'm confused as to
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I'm learning about the usage of datatypes for databases. For example: Which is better
Hi i am new to iphone and starts to learning Obj c. i have
Ruby looks a very cool language. I've started learning it for the past two
For learning purposes, I'm looking for open source Rails projects that exemplify idiomatic usage

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.