0

I'm trying to attach an existing product to an auction, but am unable to do so without first pulling the product from the database.

This code works, but how would I go about just providing the productid and then saving the auction

        var product = new Product()
        {
            //Known existing product id
            ProductId = 1
        };

        var auction = new Auction
        {
            BuyItNowPrice = 10.
            Product = product,
            ...
            ...
        };

        using (var db = new DataContext())
        {

            var product = db.Products.Find(auction.Product.ProductId);
            auction.Product = product;

            db.Auctions.Add(auction);
            db.SaveChanges();
        }

1 Answer 1

1

Include the scalar property ProductId in the Auction class

public class Auction
{
    public int Id {get;set;}
    public int ProductId {get;set;}
    public Product Product {get;set;}
    //other proerties
}

Then

auction.ProductId = 1;

db.Auctions.Add(auction);
db.SaveChanges();
Sign up to request clarification or add additional context in comments.

1 Comment

Follow up - same problem - but list of entities: stackoverflow.com/questions/7287414/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.