Hello Team,
In my MVC C# controller, I have a method that bind item with DbDataSet called MyContext,
But any time I run the project I get the following error.

private MyContext db = new MyContext();
public ActionResult Index()
{
var items = db.Items.Include(i => i.DrugGenericNames).Include(i => i.Manufacturer).OrderByDescending(i => i.ItemID);
return View(items.ToList());
}
public class MyContext : DbContext
{
public MyContext()
: base("connectionString")
{
}
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
//avoids pluralizing table names in database
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove
}
}
}
Jignesh KumarPosted Nov 29, 2024, 8:18 AM
Could you please try in onModelCreating() Method,
Emmmanuel FIADUFEPosted Nov 29, 2024, 11:47 AM
Hello Kumar
the same error still exist
Emmmanuel FIADUFEPosted Nov 29, 2024, 5:34 AM
public partial class tblManufacturer();
tblItems { get; set; }
{
public tblManufacturer()
{
this.tblItems = new HashSet
}
public int ManufactureID { get; set; }
public string ManufacturerName { get; set; }
public string Description { get; set; }
public virtual ICollection
}
public partial class tblItem();();
DrugGenericNameID { get; set; } ManufacturerID { get; set; } Category { get; set; } AlertQty { get; set; } LastUpdated { get; set; }
tblPurchaseItems { get; set; } tblStocks { get; set; }
{
public tblItem()
{
this.tblPurchaseItems = new HashSet
this.tblStocks = new HashSet
}
public int ItemID { get; set; }
public string ItemName { get; set; }
public Nullable
public Nullable
public Nullable
public Nullable
public string Description { get; set; }
public Nullable
public virtual ICollection
public virtual ICollection
public virtual tblManufacturer tblManufacturer { get; set; }
public virtual tblDrugGenericName tblDrugGenericName { get; set; }
}
public partial class tblDrugGenericName();
tblItems { get; set; }
{
public tblDrugGenericName()
{
this.tblItems = new HashSet
}
public int DrugGenericID { get; set; }
public string DrugGenericName { get; set; }
public string Description { get; set; }
public virtual ICollection
}
Jignesh KumarPosted Nov 29, 2024, 3:59 AM
Could you please share the below tables? full class, You are missing the configuration for your relationship in EF core
Emmmanuel FIADUFEPosted Nov 28, 2024, 9:26 PM
Hello Kumar
HTML
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.ItemID)
@Html.DisplayFor(modelItem => item.ItemName)
@Html.DisplayFor(modelItem => item.Manufacturer.ManufacturerName)
@Html.DisplayFor(modelItem => item.DrugGenericNames.DrugGenericName)
@Html.DisplayFor(modelItem => item.Category)
@Html.DisplayFor(modelItem => item.AlertQty)
@Html.ActionLink("Edit", "Edit", new { id = item.ItemID }) |
@Html.ActionLink("Details", "Details", new { id = item.ItemID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ItemID })
}
CONTROLLER
public ActionResult Index()
{
var items = db.Items.Include(i => i.DrugGenericNames).Include(i => i.Manufacturer).OrderByDescending(i => i.ItemID);
return View(items.ToList());
}
This is the error from the format you asked me to follow
This is the error that comes from the DbSet
Jignesh KumarPosted Nov 28, 2024, 6:07 PM
Could you please help here with your error? That will help to identify the root casue. Share the class here in which you are getting error.
Emmmanuel FIADUFEPosted Nov 28, 2024, 10:16 AM
Hello Jignesh Kumar
I crosse checked all my tables and I can say that all of them have primary,
and looking at the screenshot in the above the error is pointing to the DbSet and i tried to remodified it again but the error still exist, any further assistance will be highly appreciated.
public class MyContext : DbContext DrugGenericNames { get; set; }- Items { get; set; }
Manufacturers { get; set; } Stocks { get; set; } Suppliers { get; set; } Purchases { get; set; } PurchaseItems { get; set; } Notifications { get; set; } SalesReturns { get; set; } SalesReturnDetails { get; set; } Sales { get; set; } SalesItems { get; set; }
Users { get; set; }();
{
public ASPNETPHARMACYDBEntities db;
public MyContext()
{
db = new ASPNETPHARMACYDBEntities();
}
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
public DbSet
//avoids pluralizing table names in database
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove
}
}
Emmmanuel FIADUFEPosted Nov 27, 2024, 1:59 PM
Hello Jignesh Kumar
Please I have apply the [Key] to all the classes but the same error still exist.
Jignesh KumarPosted Nov 26, 2024, 5:24 PM
you need to check for all your table should follow the same, In your Stock class you are missing key,
You need to verify the following: If the Id column does not exist, you must designate the primary key using the [Key] annotation.
Emmmanuel FIADUFEPosted Nov 26, 2024, 12:14 PM
Hello Jignesh Kumar,
I have [Key] for the ID in the model as you can see and I didn't know the cause of the error.
public class DrugGeneric
{
[Key]
public int DrugGenericID { get; set; }
[Required]
[StringLength(50, ErrorMessage="Maximum limit exceeded")]
public string DrugGenericName { get; set; }
public string Description { get; set; }
public virtual ICollection
}
}
public class Item
{
[Key]
public int ItemID { get; set; }
[Required]
[Display(Name="Item")]
public string ItemName { get; set; }
}
public class Stock
{
public Stock()
{
BatchNo = "--";
Stop_Notification = true;
ItemExpired = false;
}
public int StockID { get; set; }
[Required]
public int ItemID { get; set; }
[StringLength(20, ErrorMessage="Too long. Plese check again!")]
public string BatchNo { get; set; }
[Range(0,9999999)]
public int InitialQty { get; set; }
}
Jignesh KumarPosted Nov 26, 2024, 10:56 AM
Hello,
You need to check primary key for you table, in your case your model name should have Id column other wise you need to mark your primary key column with [Key] attribute as below
else Your all class should follow Id column like below,
If you follow this for all remaining table then your issue will resolve
AlicessPosted Nov 26, 2024, 4:09 AM
Thanks