1

I have an mvc5 application that is connected to a EF database. Some fields in this database are meant to be autogenerated as declared in SQL, but when used in MVC and upon inserting records, the GUID only contains the value of 0 for all records. How can I resolve this? Any help will be appreciated. Thanks.

Model class:

 public partial class Store
    {
        public int StoreID { get; set; }
        public int CustomerID { get; set; }
        public string StoreName { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        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; }
    }

Both StoreUID and StoreNumber supposed to be autogenerated fields. Below is an example how its supposed to be when a new store is inserted, however currently, storeNumber and StoreUID both just return 0.

enter image description here

2 Answers2

0

This isn't really an Entity Framework feature. EF needs to be aware of these column types to generate the appropriate SQL. What you require is something that's actually achieved from the database. For Model First, I got the auto generated int Id functionality by modifying the T4 template that ships with EF to write the appropriate SQL, but it really is database functionality. StoreNumber is a different case since SQL server only allows one identity column.

For your database, your StoreUID column specification should be:

StoreUID uniqueidentifier not null DEFAULT newid()

You don't specify if you're dealing with model first or code first, or if you're building something new, so you may have to modify your existing table for this.

EDIT

If you're using model first, ensure that in your model the Store Generated Column is set to Identity for the StoreUID value to be server generated. If not, and you're not worried about who/what creates the GUID, then create a default constructor for Store, if you don't already have one. Then in there add StoreUID = Guid.NewGuid();.

For StoreNumber, SQL server doesn't support multiple columns with auto incrementing integers. You'd need to research a number of strategies for inserting it.

A number are listed here and here. Essentially make StoreNumber a function of StoreID with Computed Columns, or use an independent Sequence:

ALTER TABLE Store DROP COLUMN StoreNumber;  
GO  
ALTER TABLE Store ADD StoreNumber AS StoreID + 550000; 
reckface
  • 5,678
  • 4
  • 36
  • 62
  • Thank you. I do have the defaults in my database so I'm not sure what else is wrong. Any idea what else could be causing it to not auto-generate? –  Aug 30 '17 at 10:42
  • ahhh, in that case, ensure that you don't have values being set from EF when inserting. Try inserting manually from SQL to verify that your columns have the correct specification – reckface Aug 30 '17 at 10:48
  • I've manually inserted from SQL and it works perfectly. No idea why it's returning 0 from my application :(. How would I check if the EF is setting the value? Because it just initializes it to all zeroes weirdly –  Aug 30 '17 at 11:10
  • It's likely because initialising the new Store object, is setting default values (with the zeros) before the insert. See edit for model first approach – reckface Aug 30 '17 at 11:35
  • What do you mean by this? Can you show me an example –  Aug 30 '17 at 11:41
  • the default value for a type depends on the type. So when you create new Store, you'll have `StoreID = 0`, `StoreLogo = null`, and the the `StoreUID` will be 0000... You need to tell EF not to use the default property by telling it it's a Store Generated Column. – reckface Aug 30 '17 at 11:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153237/discussion-between-reckface-and-unicorn-girl). – reckface Aug 30 '17 at 11:44
0

You need to add defaults to your database table to generate the fields.

ALTER TABLE [dbo].[Store] ADD  DEFAULT (newid()) FOR [StoreUID]

ALTER TABLE [dbo].[Store] ADD  DEFAULT (myfuncthatreturnsanint()) FOR [StoreNumber]
jazza1000
  • 4,099
  • 3
  • 44
  • 65