1
我在sql server數據庫中有一個表。我嘗試使用實體框架將數據插入到表中,但未按預期工作。使用實體框架將數據未插入到SQL表中
表
CREATE TABLE [dbo].[UserCompletedWorkouts] (
[Id] INT NOT NULL IDENTITY(1,1),
[UsersName] VARCHAR (50) NULL,
[WorkoutId] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
型號
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<ConfirmedWorkoutModel> ViewWorkout { get; set; }
}
[Table("UserCompletedWorkouts")]
public class ConfirmedWorkoutModel
{
public int Id { get; set; }
public string UsersName { get; set; }
public int WorkoutId { get; set; }
}
}
控制器
public ActionResult Index(int Id)
{
db.ViewWorkout.Add(new ConfirmedWorkoutModel() { WorkoutId = Id, UsersName = User.Identity.Name});
return View(db.ViewWorkout.ToList());
}