0

I have an MVC application that has some CRUD functionality, I've noticed that upon creating a new record, one of the fields that supposed to be automatically generated returns 0. Is there a way I can populate this field to increment every time a new record is created. Thanks.

Test shows how StoreNumber remains 0 whenever I add records. enter image description here

Controller:

 public ActionResult Create()
        {
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName");
            return View();
        }



        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "StoreID,CustomerID,StoreName,StoreUID,StoreNumber,StoreLogo,StoreLogoPath,StoreAddress,StoreCity,StoreRegion,StoreCountry")] Store store)
        {
            if (ModelState.IsValid)
            {
                store.StoreUID = Guid.NewGuid();

                db.Stores.Add(store);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName", store.CustomerID);
            return View(store);
        }

Store Class

public partial class Store
    {
        public int StoreID { get; set; }
        public int CustomerID { get; set; }
        public string StoreName { get; set; }

        public System.Guid StoreUID { get; set; }

        public int StoreNumber { get; set; }
        public string StoreLogo { get; set; }
        public string StoreLogoPath { get; set; }
        public string StoreAddress { get; set; }
        public string StoreCity { get; set; }
        public string StoreRegion { get; set; }
        public string StoreCountry { get; set; }

        public virtual Customer Customer { get; set; }
    }

3 Answers 3

1
public partial class Store
    {
        public int StoreID { get; set; }
        public int CustomerID { get; set; }
        public string StoreName { get; set; }

        public System.Guid StoreUID { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int StoreNumber { get; set; }
        public string StoreLogo { get; set; }
        public string StoreLogoPath { get; set; }
        public string StoreAddress { get; set; }
        public string StoreCity { get; set; }
        public string StoreRegion { get; set; }
        public string StoreCountry { get; set; }

        public virtual Customer Customer { get; set; }
    }

just use DatabaseGenerated attribute to your field

Sign up to request clarification or add additional context in comments.

8 Comments

It is still returning 0
use this one modelBuilder.Entity<Store>().Property(a => a.StoreNumber ).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Yes I have management studio, where do I add that code? In the Store.cs?
public class SchoolContext: DbContext { public SchoolDBContext(): base() { } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //Configure default schema modelBuilder.HasDefaultSchema("Admin"); } }
@GareginHambardzumyan I'm pretty sure StoreID is already the identity. StoreNumber is different.
|
0
CREATE TABLE [dbo].[Test1] (
    [Id]                 INT                   NOT NULL       IDENTITY (1, 1),

You need DB Table with autoincrement

2 Comments

I'm pretty sure StoreID is already the identity. StoreNumber is different.
I think only identity is auto incremented.
0

Each table can only have one identity generated automatically. I assume this is the StoreID field. Thus, you'd have to mark StoreID with [DatabaseGenerated(DatabaseGeneratedOption.Identity)]. Or if the column is actually Id (int) and already marked with [DatabaseGenerated(DatabaseGeneratedOption.Identity)], then that means no other int fields are going to get auto updated by the built in identity features of SQL. Thus, you have to manually set the number as there is nothing updating that for you.

1 Comment

Ah I understand, how can I manually set the number ? Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.