Reputation: 23
I attached the context for the model and controller. I couldn't get why I got this error:
CS1061 does not contain a definition for
This is the DbContext
:
public virtual DbSet<Eng_Designation_Name> Eng_Designation_Name { get; set; }
modelBuilder.Entity<Eng_Designation_Name>(entity =>
{
entity.HasKey(e => e.Designation_Names_Id);
entity.ToTable("Eng_Designation_Name");
entity.Property(e => e.Designation_Names_Id)
.HasColumnName("Designation_Names_Id");
entity.Property(e => e.Designation_Names)
.HasMaxLength(250)
.IsUnicode(false)
.HasColumnName("Designation_Names");
entity.Property(e => e.AddDate)
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.AddBy)
.HasMaxLength(250)
.IsUnicode(false)
.HasColumnName("AddBy");
Model class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace TEEPOEEApp.Models
{
public partial class Eng_Designation_Name
{
public Eng_Designation_Name()
{
Eng_DownTimeDay_Designation_Name = new HashSet<Eng_DownTimeDay_Designation_Name>();
}
[Key]
public int Designation_Names_Id { get; set; }
[StringLength(500)]
public string Designation_Names { get; set; }
[Column(TypeName = "datetime")]
public DateTime AddDate { get; set; }
[StringLength(250)]
public string AddBy { get; set; }
[InverseProperty("Designation_Names")]
public virtual ICollection<Eng_DownTimeDay_Designation_Name> Eng_DownTimeDay_Designation_Name { get; set; }
}
}
Controller:
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TEEPOEEApp.Models;
namespace TEEPOEEApp.Controllers
{
public class Eng_Designation_Name : Controller
{
private readonly TEEPOEEAppDBContext _context;
public Eng_Designation_Name(TEEPOEEAppDBContext context)
{
_context = context;
}
// GET: Eng_Designation_Name
public async Task<IActionResult> Index()
{
return View(await _context.Eng_Designation_Name.ToListAsync());
}
// GET: Eng_Designation_Name/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null || _context.Eng_Designation_Name == null)
{
return NotFound();
}
var eng_Designation_Name = await _context.Eng_Designation_Name
.FirstOrDefaultAsync(m => m.Designation_Names_Id == id);
if (eng_Designation_Name == null)
{
return NotFound();
}
return View(eng_Designation_Name);
}
// GET: Eng_Designation_Name/Create
public IActionResult Create()
{
return View();
}
// POST: Eng_Designation_Name/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Designation_Names_Id,Designation_Names,AddBy,AddDate")] Eng_Designation_Name eng_Designation_Name)
{
if (ModelState.IsValid)
{
eng_Designation_Name.AddDate = DateTime.Now;
eng_Designation_Name.AddBy = User.Identity.Name;
_context.Add(eng_Designation_Name);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(eng_Designation_Name);
}
// GET: Eng_Designation_Name/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null || _context.Eng_Designation_Name == null)
{
return NotFound();
}
var eng_Designation_Name = await _context.Eng_Designation_Name.FindAsync(id);
if (eng_Designation_Name == null)
{
return NotFound();
}
return View(eng_Designation_Name);
}
// POST: Eng_Designation_Name/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Designation_Names_Id,Designation_Names,AddBy,AddDate")] Eng_Designation_Name eng_Designation_Name)
{
if (id != eng_Designation_Name.Designation_Names_Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
eng_Designation_Name.AddDate = DateTime.Now;
eng_Designation_Name.AddBy = User.Identity.Name + " Edit";
_context.Update(eng_Designation_Name);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Eng_Designation_NameExists(eng_Designation_Name.Designation_Names_Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(eng_Designation_Name);
}
// GET: Eng_Designation_Name/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null || _context.Eng_Designation_Name == null)
{
return NotFound();
}
var eng_Designation_Name = await _context.Eng_Designation_Name
.FirstOrDefaultAsync(m => m.Designation_Names_Id == id);
if (eng_Designation_Name == null)
{
return NotFound();
}
return View(eng_Designation_Name);
}
// POST: Eng_Designation_Name/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
if (_context.Eng_Designation_Name == null)
{
return Problem("Entity set 'TEEPOEEAppDBContext.Eng_Designation_Names' is null.");
}
var eng_Designation_Name = await _context.Eng_Designation_Name.FindAsync(id);
if (eng_Designation_Name != null)
{
_context.Eng_Designation_Name.Remove(eng_Designation_Name);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool Eng_Designation_NameExists(int id)
{
return _context.Eng_Designation_Name.Any(e => e.Designation_Names_Id == id);
}
// POST: Eng_Designation_Name/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Designation_Names_Id,Designation_Names,AddBy,AddDate")] Eng_Designation_Name eng_Designation_Name)
{
if (ModelState.IsValid)
{
eng_Designation_Name.AddDate = DateTime.Now;
eng_Designation_Name.AddBy = User.Identity.Name;
_context.Add(eng_Designation_Name);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(eng_Designation_Name);
}
}
}
I tried to add another table on the model and run the app, but I got the following errors:
Error CS1061
'Eng_Designation_Name' does not contain a definition for 'AddDate' and no accessible extension method 'AddDate' accepting a first argument of type 'Eng_Designation_Name' could be found (are you missing a using directive or an assembly reference?) TEEPOEEApp C:\Users\londono-ecr\source\repos\TEEPOEEApp\TEEPOEEApp\Controllers\Eng_Designation_NameController.cs 65 ActiveError CS1061
'Eng_Designation_Name' does not contain a definition for 'AddBy' and no accessible extension method 'AddBy' accepting a first argument of type 'Eng_Designation_Name' could be found (are you missing a using directive or an assembly reference?) TEEPOEEApp C:\Users\londono-ecr\source\repos\TEEPOEEApp\TEEPOEEApp\Controllers\Eng_Designation_NameController.cs 66 Active
I don't understand very well what is the reason. Thank you very much for your help.
Upvotes: 2
Views: 266
Reputation: 28499
Both your Controller and your Model classes have the same name Eng_Designation_Name
. This causes confusion in the code.
Either use two different names.
For example, specifically add "Controller" to the name of the Controller class:
namespace TEEPOEEApp.Controllers
{
public class Eng_Designation_Name_Controller : Controller
Note that it is generally a convention in .net ASP MVC that the name of a Controller class should end with "Controller".
Also note that in c#, as a convention, we generally don't use underscores within identifiers. This will not cause errors, but might lead to unhappy faces when experienced C# developers have to work with the code.
Or specify the full namespace for the type when using it and there can be misunderstandings.
For example here:
public async Task<IActionResult> Create
([Bind("Designation_Names_Id,Designation_Names,AddBy,AddDate")]
Eng_Designation_Name eng_Designation_Name)
The compiler understands Eng_Designation_Name
to mean the Controller class with that name, not the Model. Use the full namespace for the type to make clear you mean the Model:
public async Task<IActionResult> Create
([Bind("Designation_Names_Id,Designation_Names,AddBy,AddDate")]
TEEPOEEApp.Models.Eng_Designation_Name eng_Designation_Name)
Upvotes: 0
Reputation: 137
Specify the column name for AddDate in this line in your DbContext class:
entity.Property(e => e.AddDate)
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
Upvotes: 1