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 KumarRank #91 · 22.9K reputationPosted Nov 29, 2024, 3:18 AM EST
Could you please try in onModelCreating() Method,
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity() .HasMany(m => m.tblItems) .WithOptional(i => i.tblManufacturer) .HasForeignKey(i => i.ManufacturerID); modelBuilder.Entity() .HasOptional(i => i.tblDrugGenericName) .WithMany(d => d.tblItems) .HasForeignKey(i => i.DrugGenericNameID); modelBuilder.Entity() .HasMany(i => i.tblPurchaseItems) .WithRequired(p => p.tblItem) .HasForeignKey(p => p.ItemID); modelBuilder.Entity() .HasMany(i => i.tblStocks) .WithRequired(s => s.tblItem) .HasForeignKey(s => s.ItemID); base.OnModelCreating(modelBuilder); }Jignesh KumarRank #91 · 22.9K reputationPosted Nov 26, 2024, 5:56 AM EST
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
public class MyEntity { [Key] public int MyEntityId { get; set; } public string Name { get; set; } }else Your all class should follow Id column like below,
public class tblNotification { public int Id { get; set; } // other columns here }public class tblPurchaseItem { public int Id { get; set; } // other columns here }If you follow this for all remaining table then your issue will resolve
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 26, 2024, 12:24 PM EST
you need to check for all your table should follow the same, In your Stock class you are missing key,
public class Stock { public Stock() { BatchNo = "--"; Stop_Notification = true; ItemExpired = false; } [Key] 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; } }You need to verify the following: If the Id column does not exist, you must designate the primary key using the [Key] annotation.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 26, 2024, 7:14 AM EST
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; }
}
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 27, 2024, 8:59 AM EST
Hello Jignesh Kumar
Please I have apply the [Key] to all the classes but the same error still exist.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 28, 2024, 5:16 AM EST
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
}
}
Alicess5 reputationPosted Nov 25, 2024, 11:09 PM EST
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 28, 2024, 1:07 PM EST
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.
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove(); modelBuilder.Entity().ToTable("YourSQLTableMappedToClass").HasKey("PrimaryKeyColumnName"); //Do for all table here }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 28, 2024, 4:26 PM EST
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 KumarRank #91 · 22.9K reputationPosted Nov 28, 2024, 10:59 PM EST
Could you please share the below tables? full class, You are missing the configuration for your relationship in EF core
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 29, 2024, 12:34 AM EST
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
}
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 29, 2024, 6:47 AM EST
Hello Kumar
the same error still exist