Necesito salvar el ID del usuario que ha hecho login en la columna de nombre "CreatedBy", pero no logro escribir la query correcta.
Cada que viene creada una entidad "FitnessGoal" se salvara en el DB los datos de dicha identidad mas el ID del Usuario:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Goal,StartDate,FinishDate,Weight")] FitnessGoals fitnessGoals)
{
var user = UserManager.FindById(User.Identity.GetUserId());
if (ModelState.IsValid)
{
db.FitnessGoals.Add(fitnessGoals);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(fitnessGoals);
}
Clase FitnessGoal:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FitnessWebApplication.Models
{
[Table("FitnessGoals")]
public class FitnessGoals
{
/*public List<string> Goals ;
public FitnessGoals() { this.Goals.Add("Bulk"); this.Goals.Add("Cut"); }*/
[Key]
public int ID { get; set; }
public string CreatedBy { get; set; }
public string Name { get; set; }
public string Goal { get; set; }
[DataType(DataType.Date)]
[Column(TypeName = "DateTime2")]
public DateTime StartDate { get; set; }
[Column(TypeName = "DateTime2")]
[DataType(DataType.Date)]
public DateTime FinishDate { get; set; }
public int Weight { get; set; }
public enum Goals
{
Bulk,
Cut
}
}
}