D
D
Dmitry Gavrilenko2015-04-27 17:47:50
.NET
Dmitry Gavrilenko, 2015-04-27 17:47:50

How to form the correct Code First?

There are entity classes

class Transaction
    {
        public int ID { get; set; }
        public Customer Customer { get; set; }
        public DateTime Date { get; set; }
        public double Price { get; set; }
        public int Discount { get; set; }
        public double Ended { get; set; }
    }
    class Customer
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Last { get; set; }
        public string Other { get; set; }
    }

This model is implemented for the sales application. A purchase is made, for a certain amount, a buyer is sought and through
Transaction trans = new Transaction { Customer = cust, Date = DateTime.Now, Discount = discount, Price = price, Ended = ended };

data is added to the database. Everything works with a bang, but looking at the database, I saw that
4d0d0d96e5924c0d84f16a5bacde017f.PNG661a933b8eeb47928c9dc83fda127127.PNG
How to solve?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
wkololo_4ever, 2015-04-27
@wkololo_4ever

Your instance of the Customer class is not tracked by EF. All code is needed. Try using Attach()

V
Vladimir S, 2015-04-27
@hePPer

add a property to the Customer class
for feedback

C
Cyril, 2015-04-28
@teoadal

public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question