1

Recently I have faced with an issue in mvc. My problem is about how increment a value of field which is unique,primary and int. For example , I have a customerCode as int and I want to increment a value of that after saving new customer . I get that mvc first check id and if its equal to zero ,then increment it to max+1. But the issue is here: if I want to increment it like Max+4, how can I handle it? this is my code:

public ActionResult Save(Customers customer)
{
    if (!ModelState.IsValid)
    {
        var _Customer = new CreatnewCustomerViewModel()
        {
            Customer = customer,
            membershiptype = _context.membershiptype.ToList()
        };
        return View("CustomerForm", _Customer);
    }
    if (customer.CustomerID==0)
    {
        //int id = _context.customers.Max(m => m.CustomerID);
        //customer.CustomerID = id + 1;
        _context.customers.Add(customer);
    }
    else
    {
        var CustomerIndb = _context.customers.Single(c => c.CustomerID == customer.CustomerID);
            {
            CustomerIndb.Name = customer.Name;
            CustomerIndb.birthday = customer.birthday;
            CustomerIndb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
            // CustomerIndb.MembershipType = customer.MembershipTypeID;
            CustomerIndb.MembershipTypeID = customer.MembershipTypeID;
            }



    }
    _context.SaveChanges();
    return RedirectToAction("index", "Customer");
}
Ali Eshghi
  • 1,131
  • 1
  • 13
  • 30
  • 2
    Let the database do it for you, not the code. Use an `identity` column and specify it a such in your entity mapping using `DatabaseGeneratedOption.Identity`. – Igor Sep 18 '18 at 18:40
  • @Igor Can you guid me more? I am using sql server ,how can I use identity andDatabaseGeneratedOption.Identity . where should I use them? – Ali Eshghi Sep 18 '18 at 18:45
  • https://stackoverflow.com/questions/14612813/entity-framework-code-first-using-one-column-as-primary-key-and-another-as-auto – Igor Sep 18 '18 at 18:59
  • 2
    Possible duplicate of [Entity Framework Code First Using One column as Primary Key and another as Auto Increment Column](https://stackoverflow.com/questions/14612813/entity-framework-code-first-using-one-column-as-primary-key-and-another-as-auto) – Igor Sep 18 '18 at 19:03
  • thanks dear @Igor, I read that link but I can't get. I will research more... – Ali Eshghi Sep 18 '18 at 19:22
  • How are you using Entity Framework? Are you using Code First or Database First mode? – ADyson Sep 18 '18 at 19:32
  • @ADyson CodeFirst. and using migration. – Ali Eshghi Sep 18 '18 at 19:34
  • @AliEshghi Be more specific while asking question! Tell me for which column you want `Identity Increment` by 4. Is it `CustomerID` column which is primary key or any other column named `CustomerCode` which is identity column but not primary key? – TanvirArjel Sep 19 '18 at 02:42

2 Answers2

2

Write your Customers model class as follows:

public class Customers
{
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int CustomerID { get; set; }

   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int CustomerCode { get; set; }

   public string Name { get; set; }

  // Other properties

}

Now generate a new migration and update the database accordingly!

Now if you want to set Identity Increment more than 1, then go to the database with SQL Server Management Studio, open the the Customers table. Right click on CustomerCode column and then select properties. Now set the Identity Increment value (default is 1) to your desired value.

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
0

you can easily solve that problem with the help of DatabaseGeneratedOption.Identity and Sql() method in the Up() .

If you new to both of above concepts , Here is the links with clear instructions

https://forums.asp.net/t/2062551.aspx?Entity+Framework+How+to+set+a+start+value+for+an+Auto+Incremented+Column+

Addtional: How to set initial value for auto incremented property (DatabaseGeneratedOption.Identity)

Hope it will help to get rid of the problem, let me know your status.

Thanks

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12