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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:59:22+00:00 2026-06-02T08:59:22+00:00

In my current project (which is really small) i have 3 tables/ POCO entities

  • 0

In my current project (which is really small) i have 3 tables/POCO entities which i would like to manipulate using EF.

The Tables are:

  1. Status (contains status details)
  2. StatusStatusType (which is needed because of the many-2-many relationship)
  3. StatusType (A table which is used to group statuses by type)

I now want to create a new Status in the database and user code like you see below

//Create new status (POCO) entity
var newStatus = new Status {
    StatusId = status.Id,
    UserId = user.Id,
    Text = status.text,
    CreateDate = DateTime.Now
};

// Persist need status to database
using (var db = new demoEntities())
{
    db.Statuses.AddObject(newStatus);
    db.SaveChanges();
}

This code works fine but i want to also set StatusType of the status entity. All possible status types are already included in the StatusType table. I don’t want to create new statuses only create a reference.

I figured i should use something like :

status.StatusTypes == "new";

Update 22-04-2012 13:31

The example is simplified and spans multiple projects within a solution. Because of this i prefer not to use code within the create section (e.g. demoEntities).I do however know the PK of the status i need to reference.

  • 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-02T08:59:25+00:00Added an answer on June 2, 2026 at 8:59 am

    If you know that your status type already exists you also must now its primary key. Once you have a primary key value you can use this approach:

    var newStatus = new Status {
        StatusId = status.Id,
        UserId = user.Id,
        Text = status.text,
        CreateDate = DateTime.Now
    };
    
    // Just dummy object for existing status type
    var existingStatusType = new StatusType {
        Id = existingStatusTypeId
    };
    
    // Persist need status to database
    using (var db = new demoEntities())
    {
        db.Statuses.AddObject(newStatus);
        // First let EF know that the status type already exists
        // Attaching prior to making relation is important!
        db.StatusTypes.Attach(existingStatusType);
        // Now make relation between new and existing entity 
        newStatus.StatusTypes.Add(existingStatusType);
        db.SaveChanges();
    }
    

    If you don’t want to create relation inside of the persistence code you must use little bit different approach.

    var newStatus = new Status {
        StatusId = status.Id,
        UserId = user.Id,
        Text = status.text,
        CreateDate = DateTime.Now
    };
    
    // Just dummy object for existing status type
    var existingStatusType = new StatusType {
        Id = existingStatusTypeId
    };
    
    newStatus.StatusTypes.Add(existingStatusType);
    
    // Persist need status to database
    using (var db = new demoEntities())
    {
        // This will add both newStatus and existingStatusType as new entities
        db.Statuses.AddObject(newStatus);
        // You must fix it to make sure that existingStatusType is not inserted 
        // to database again
        status.StatusTypes.ForEach(st =>
            db.ObjectStateManager.ChangeObjectState(st, EntityState.Unchanged));
        db.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my current project I am using a 3rd party library which has no
I have a requirement on my current project (a Flex app which will be
BACKGROUND: I would like to have small labels in columns of a table. I'm
I have a current build process which does a .NET build using MSBuild and
In a current (C#) project we have a 3rd party assembly that contains a
I am looking to use an IOC container in my current project which is
I'm trying to create a build script for my current project, which includes an
I am interested in applying dependency injection to my current project, which makes use
So, in my current project, when you navigate to the team page, which has
Our flex project, which works fine in its current environment with coldfusion 7 single

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.