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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:24:24+00:00 2026-06-01T07:24:24+00:00

I have a model Product with a 1:n relationship with model Product_Tag. I also

  • 0

I have a model Product with a 1:n relationship with model Product_Tag. I also have a model Tag has a 1:n relationship with Product_Tag.

class Product{
  int ID {get;set;}
  public virtual ICollection<Product_Tag> productTag {get;set;}
}

class Tag {
  int ID {get;set;}
  public virtual ICollection<Product_Tag> productTag {get;set;}

}

class Product_Tag {
  int ID {get;set;}
  int ProductID{get;set;}
  int TagID {get;set;}
  public virtual Product product {get;set;}
  public virtual Tag tag {get;set;}
}

When saving a new Product, I want to be able to save any associations to Tags in Product_Tags. Product_Tag requires a PRoductID and a TagID. I have the TagID’s available at save time, but the ProductID will come from thew newly-created Product. How to get the newly-created Product’s ID immediately after saving the new Product?

Alternatively, is there a way to just save the Product model with its productTags having only TagID’s and get DbContext to fill in the ProductID for me?

  • 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-01T07:24:26+00:00Added an answer on June 1, 2026 at 7:24 am

    When you add a new entity to the database, its ID will be automatically populated by Entity Framework. You can use it.

    using (var context = new MyDbContext())
    {
        var product = new Product();
        product.Name = "My product";
    
        context.Products.Add(product);
        context.SubmitChanges();
    
        var productId = product.ID; // here you can get ID of added product
    }
    

    Also, EF can work with ‘many-to-many’ relationships, and you don’t need to create additional class for relationship itself. If you use code first, you can use this mapping:

    public class Product
    {
        int ID {get;set;}
        public virtual ICollection<Tag> Tags {get;set;}
    }
    
    public class Tag
    {
        int ID {get;set;}
        public virtual ICollection<Product> Products {get;set;}
    }
    
    // In your DbContext class:
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>().HasMany(x => x.Tags).WithMany(x => x.Products)
            .Map(m =>
            {
                m.ToTable("Product_Tag"); // Relationship table name
                m.MapLeftKey("ProductID"); // Name of column for product IDs
                m.MapRightKey("TagID"); // Name of column for tag IDs
            });
    }
    

    Then, you can do something like this:

    using (var context = new MyDbContext())
        {
            var product = new Product();
            product.Name = "My product";
            product.Tags.Add(new Tag {Name = "furniture"});
            product.Tags.Add(new Tag {Name = "food"});
    
            context.Products.Add(product);
            context.SubmitChanges();
        }
    

    In this case EF automatically creates a new product, two tags and two relationships between created product and tags.

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

Sidebar

Related Questions

public class Client { public Int32 ClientID { get; set; } public virtual ICollection<Inquiry>
I have these classes: public class Product { [Key] public virtual int ProductId {
I have a model relationship with Product hasmany Page. When I add a new
I have Product model and it has many categories with a has_many :through association
i've a problem with my tabularinline field. I have model like this class Product(models.Model):
I have a model called Event and another called Product. An event has many
I have a Product entity that has a foreign key relationship to Manufacturer. I
If i have the following classes public class Order { public virtual ISet<OrderItem> OrderItems
If i have declared entity relationship in my model as virtual then there is
I have a model like this Product ---> Options (One-to-many relationship, no -invers) Option

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.