0

I have a table that contains the following structure:

ID
Date (today's date/time)
Payment (user defined)
PreviousBal (referenced from another table prior to update)
LayawayID (link to above record)

I am new to Linq, Entity Framework and C#. I would normally write an SQL update statement to easily take care of all of this in one swoop, but in my work with MVC, I've only seen how to update and add to a table based on ALL fields being user inputs.

So my question would be, how do I set a column in the database to a value that is not user defined?

Also, can Linq and EF even handle pulling a value from another table and setting the column value to the result?

Update To Clarify, this is how I have been creating tables thus far in my application

        [HttpPost]
    public ActionResult CreateCustomer(Customers Customer)
    {
        if (ModelState.IsValid)
        {
            _db.Customers.Add(Customer);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(Customer);
    }

In that case, the user has the ability to add ALL columns in the customer table with the exception of the primary key. My confusion comes in with the case where there are columns in my table that need to be set to existing data rather than user input.

MrMonkey82
  • 13
  • 3

1 Answers1

0

You can easily add properties to the instance of the Customers class you're using.

Adding something like today's date is usually done on the database level.
Most databases can assign default values to columns (example: mysql automatically store record creation timestamp)

For the values that come from a different table, you will have to retrieve them in your controller (in the CreateCustomer function) and add them to the model.
However, if you are using an MVC framework, you will see that this is usually never done that way, instead you only reference an ID of the model that you want to reference.
One thing that you should always avoid is to have duplicates of the same thing (like the values of whatever you are referencing) in different tables in your database.

Community
  • 1
  • 1
Matt Ko
  • 969
  • 7
  • 14
  • Thanks for the response. When you say "you only reference the ID of the model you want to reference" would that be something like "public ICollection ID { get; set; }" in the model" – MrMonkey82 Oct 12 '14 at 14:43
  • Also, I didn't know you could autopop the date from the db level. That's very useful. Thanks. – MrMonkey82 Oct 12 '14 at 14:50
  • Sorry for the late response, I'm only getting used to StackOverflow... You don't actually need to keep track of the ID yourself. The MVC framework will do that for you. However, you do need to reference the 'PreviousBal' object in your 'Customer' model. If you adapt your controller, you can set the reference automatically every time you create/update your 'Customer' (without having to get it from user input). – Matt Ko Oct 25 '14 at 18:05
  • no worries. I'm still getting used to the code first approach. I'm used to having to set all of my fields in the code, such as (set layawaydata.layawayid = layaway.id). I've been tinkering around with it and I think I'm starting to understand how the relationships between tables works. Thanks again for the explanation. – MrMonkey82 Oct 26 '14 at 17:19